Forum Moderators: coopster

Message Too Old, No Replies

looping array results

         

mooger35

4:18 pm on May 10, 2006 (gmt 0)

10+ Year Member



I've been fooling around with arrays trying to become more comfortable with them and I've run into a little roadblock.


$data[] = array('volume' => 67, 'edition' => 2, 'test' => 1);
$data[] = array('volume' => 86, 'edition' => 1, 'test' => 9);
$data[] = array('volume' => 85, 'edition' => 6, 'test' => 11);
$data[] = array('volume' => 98, 'edition' => 2, 'test' => 23);
$data[] = array('volume' => 86, 'edition' => 6, 'test' => 35);
$data[] = array('volume' => 67, 'edition' => 7, 'test' => 13);

foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
$test[$key] = $row['test'];
}
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);

This gives me my array and will sort it but now I'd like to echo the results like this

echo $row['volume']." ¦ ".$row['edition']." ¦ ".$row['test']."<br />";

How do I loop that so I see all the information? Normally I'd use while with database results... will that work here as well?

coopster

6:01 pm on May 10, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could use a while [php.net] loop, but foreach [php.net] works great for traversing arrays. It automatically resets the pointer to the beginning for you and everything. Plus, you already used the same logic to do your sorting, now just copy that control structure and wrap it around your echo:
foreach ($data as $key => $row) { 
echo $row['volume']." ¦ ".$row['edition']." ¦ ".$row['test']."<br />";
}

mooger35

6:05 pm on May 10, 2006 (gmt 0)

10+ Year Member



Thanks Coopster,

I actually came back to post that I found it and I'm happy to say it is just what you suggested.

Not sure why I didn't think of redoing the foreach loop originally.

mooger35

6:19 pm on May 10, 2006 (gmt 0)

10+ Year Member



sorry... one more question.

lets say I only want to show the top 3?

something like this:
for ($i = 1; $i <= 3; $i++) {
echo blah blah blah
}

How do I incorporate that with the foreach loop?

coopster

6:22 pm on May 10, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



initialize a counter at zero, and if it hits three you can break.
$count = 0; // initialize 
foreach ($data as $key => $row) {
echo $row['volume']." ¦ ".$row['edition']." ¦ ".$row['test']."<br />";
if (++ [php.net]$count == 3) {
break [php.net];
}
}

mooger35

6:36 pm on May 10, 2006 (gmt 0)

10+ Year Member



perfect. thanks.

Final thing and then I'm done... I know I said that last time. :-)

while ($row_playerlist = mysql_fetch_assoc($playerlist)){
$playerid = $row_playerlist['playerid'];
$firstname = $row_playerlist['firstname'];
$lastname = $row_playerlist['lastname'];
$player[] = array('playerid' => $playerid, 'firstname' => $firstname, 'lastname' => $lastname);
}

foreach ($player as $key => $row) {
$playerid[$key] = $row['playerid'];
$firstname[$key] = $row['firstname'];
$lastname[$key] = $row['lastname'];
}
array_multisort($firstname, SORT_ASC, $lastname, SORT_ASC, $player);

$count = 0;
foreach ($player as $key => $row) {
echo $row['playerid']." ¦ ".$row['firstname']." ¦ ".$row['lastname']."<br />\n";
if (++$count == 3) { break; }
}

For some reason I'm getting this error message:

Warning: array_multisort(): Argument #1 is expected to be an array or a sort flag in blah blah blah on line 19

It worked before, but for some reason not now?

[edited by: coopster at 6:43 pm (utc) on May 10, 2006]
[edit reason] fixed sidescroll [/edit]

coopster

6:47 pm on May 10, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm guessing that your query returned no data. Therefore no variables or arrays are being populated yet you are trying to sort them.

mooger35

6:49 pm on May 10, 2006 (gmt 0)

10+ Year Member



Nope... in fact after the message I get the 3 results sorted by the playerid field.

like this:

Warning: yadda yadda yadda 
0001 ¦ John ¦ smith
0002 ¦ Joe ¦ johnson
0003 ¦ Mike ¦ woods

coopster

6:56 pm on May 10, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It's because you define the variable as a string in your while loop and then are trying to change each index of the string again in your for loop. Either use a different variable name or reset the variables.
while ($row_playerlist = mysql_fetch_assoc($playerlist)){ 
$playerid = $row_playerlist['playerid'];
$firstname = $row_playerlist['firstname'];
$lastname = $row_playerlist['lastname'];
$player[] = array('playerid' => $playerid, 'firstname' => $firstname, 'lastname' => $lastname);
}
$playerid = array();
$firstname = array();
$lastname = array();

foreach ($player as $key => $row) {
$playerid[$key] = $row['playerid'];
$firstname[$key] = $row['firstname'];
$lastname[$key] = $row['lastname'];
}
array_multisort($firstname, SORT_ASC, $lastname, SORT_ASC, $player);

mooger35

7:02 pm on May 10, 2006 (gmt 0)

10+ Year Member



so simple... thank you.


/now I'm done. :-)

coopster

7:09 pm on May 10, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No problem, I should explain a bit more what was happening to you there though -- not just for you, but for future readers as well. You see, when you first set that variable you were defining it as a string. Well, strings are also zero-based arrays. Think of a string as an array of characters. So by changing the value at the $key index of the already established string variable you were merely modifying that part of the string. Then array_multisort() gets passed the string and was expecting an array. An example might demonstrate things more clearly:
$firstname = 'mooger'; 
print "$firstname<br />"; //
$firstname[0] = 'M';
$firstname[1] = 'O';
$firstname[2] = 'O';
print "$firstname<br />";
// will output:
mooger
MOOger

More reading on the PHP Strings [php.net] page.