Jump to content
Larry Ullman's Book Forums

Recommended Posts

I'm an engineer by education and a programmer by hobby. I bought this book to help me learn OOP with php and it has proven to be an extremely valuable resource. One topic that is introduced early in the book is sorting multidimensional arrays. I've always handled multidimensional arrays a little differently than what is introduced in the book so I wonder if I'm doing something wrong or something too 'old school'. Using the example on page 5, I would create arrays like this:

 

$ar[0][0] = 256;

$ar[1][0] = "Jon";

$ar[2][0] = 98.5;

 

$ar[0][1] = 2;

$ar[1][1] = "Vance";

$ar[2][1] = 85.1;

 

$ar[0][2] = 9;

$ar[1][2] = "Stephen";

$ar[2][2] = 94.0;

 

To sort, I use the array_multisort php statement.

 

So my question, aside from some extra typing, what's the difference between using the above approach versus the method in the book ?

 

array(StudentID => array('name' => 'Name', grade = xx.x))

 

Aren't they both multidimensional arrays ?

Link to comment
Share on other sites

They are both multidimensional arrays, yes. There's a big difference on how you structure your array, though. You assign each value for a single student to three keys in the base arrays. They are thus not logically connected to each other. You basically create three separate value arrays assigned to $arr[0 -> 2] instead of creating three students in $arr[0 -> 2].

 

The array you create would look like the following in "Larry's style":

$arr = array(
   0 => array(256, 2, 9),
   1 => array("Jon", "Vance", "Stephan"),
   2 => array(98.5, 85.1, 94.0)
);

A structure more comparable to the example would be to assign the array like this:

$ar[0][0] = 256;
$ar[0][1] = "Jon";
$ar[0][2] = 98.5;

$ar[1][0] = 2;
$ar[1][1] = "Vance";
$ar[1][2] = 85.1;

$ar[1][0] = 9;
$ar[1][1] = "Stephan";
$ar[1][2] = 94.0;

That structure would be equal to:

$arr = array(
   0 => array(256, "Jon", 98.5),
   1 => array(2, "Vance", 85.1),
   2 => array(9, "Stephan", 94.0)
);

Keep in mind that the version is still not comparable to Larry's example. The difference is the indexing of the array. Both versions above are based on a standard zero-to-Nth indexes, while Larry indexes according to the student numbers. To talk about why he might want to do that, the foreach-structure allows for mapping arrays to KEY => VALUE pars. You could thus ouput the array like this:

foreach ( $array as $studentNumber => $student )
{
   echo "{$studentNumber}: {$student[1]}"; // Student[1] === "the name".
}

// But, since we have student number in the array above, we could get it that way:
foreach ( $array as $student )
{
   echo "{$student[0]}: {$student[1]}"; // S[0]: "the Stud.num" — s[1] === "the name".
}

The point of grouping all student info together inside a second array (as explained, you build three value arrays now) is that your sorting might mix the key together. If you sort the grades, the names will be out of sync in your version. "Jon" might therefor get a credit of 85.1 instead of the 98.5 he deserves... And that would be bad. ;)

 

If you really prefer your version, and have at least PHP 5.4 installed, you can now also use a more declarative array syntax. Without a version >= PHP 5.4, you'd simply replace [V, ... ,V] with array(V, ... ,V). I would suggest you do something along the lines of:

// Zero-to-Nth key mapping
$arr[0] = [256, "Jon", 98.5];
$arr[1] = |2, "Vance", 85.1];
$arr[2] = [9, "Stephan", 94.0];

// Student num key mapping
$arr[256] = ["Jon", 98.5];
$arr[2]   = ["Vance", 85.1];
$arr[9]   = ["Stephan", 94.0];

// Without PHP 5.4
$arr[256] = array("Jon", 98.5);

The next point is associate keys versus integer mapping. In other languages, such as Java, C# or regular C++ (which PHP is largely based on) normal arrays MUST be mapped to integers. If you want to assign values to a key like "name", ("name" => "Jon") you will need to use a structure called a hash map. PHP simplifies this by allowing both Strings, integers and other primitive types to be used as keys in the same array. The point of this is to simplify the structure a bit.

 

We could thus assign the student array as $arr[N] = ["name" => ..., "grade" => ...  ]; This will look like the following:

$arr[256] = ["name"=> "Jon", "grade" => 98.5];
$arr[2]   = ["name"=>"Vance", "grade" => 85.1];
$arr[9]   = ["name"=>"Stephan", "grade" => 94.0];

When we foreach that array, we can thus get the values this way:

foreach ( $array as $studentNum => $student )
{
   echo "#{$studentNum}: {$student['name']} ({$student['grade']})"; 
   // First iteration of $arr output: #256: Jon (98.5)
}

Hope that clears up some confusion.

Edited by Antonio Conte
  • Upvote 1
Link to comment
Share on other sites

I demand "voice-to-code" functionality, Larry. And I demand it for early next week!

 

 

Jon: You can often find your post in the forum cache. There's a "restore post" option somewhere. My workflow is always to press CMD + A and CMD + C to save the post before positing. You are the problem here, old man!

 

:P

 

If anyone didn't get that, I'm only joking of course.

 

  • Upvote 1
Link to comment
Share on other sites

Not that I'm not insensitive to how much of a PITA this is and that you shouldn't have to go through all that extra work when trying to help others out...I should be clear about that. 

 

In my day job, the ticketing system we use ranges in quality from terrible to poor, depending upon the day. For anything other than the simplest of replies, using a separate text editor and then pasting the completed reply within its interface is the only reliable option.

Link to comment
Share on other sites

 Share

×
×
  • Create New...