Reputation: 153
So this is driving me nuts. I need to pull my table rows from mySQL and then sort them but then i need to output them back as single arrays. Mostly because the code after this is written to accept that.
Here is my code. please let me know if you have any suggestions.
<?php
include 'connect.php';
$query = mysql_query("SELECT * FROM users");
while ($data = mysql_fetch_assoc($query))
{
$newarray[]=$data;
$dbusername = $data['username'];
$dbpassword = $data['password'];
$logid = $data['id'];
print_r ($data);
echo "<br/>";
}
foreach ($newarray as $key => $row) {
$volume[$key] = $row['password'];
}
array_multisort($volume, SORT_ASC, $newarray);
print_r($newarray);
?>
The result of this is:
Array (
[0] => Array ( [id] => 4 [rating] => 18 [password] => 1981 [username] => 212060064)
[1] => Array ( [id] => 2 [rating] => 6 [password] => 1983 [username] => 212060062)
[2] => Array ( [id] => 3 [rating] => 5 [password] => 1984 [username] => 212060063)
[3] => Array ( [id] => 1 [rating] => 3 [password] => 1988 [username] => 212060061)
)
However I need to output them like this:
Array ( [id] => 4 [rating] => 18 [password] => 1981 [username] => 212060064)
Array ( [id] => 2 [rating] => 6 [password] => 1983 [username] => 212060062)
Array ( [id] => 3 [rating] => 5 [password] => 1984 [username] => 212060063)
Array ( [id] => 1 [rating] => 3 [password] => 1988 [username] => 212060061)
Upvotes: 1
Views: 1113
Reputation: 5442
According to your speech:
The result of this is:
Array ( [0] => Array ( [id] => 4 [rating] => 18 [password] => 1981 [username] => 212060064) [1] => Array ( [id] => 2 [rating] => 6 [password] => 1983 [username] => 212060062) [2] => Array ( [id] => 3 [rating] => 5 [password] => 1984 [username] => 212060063) [3] => Array ( [id] => 1 [rating] => 3 [password] => 1988 [username] => 212060061) )
However I need to output them like this:
Array ( [id] => 4 [rating] => 18 [password] => 1981 [username] => 212060064) Array ( [id] => 2 [rating] => 6 [password] => 1983 [username] => 212060062) Array ( [id] => 3 [rating] => 5 [password] => 1984 [username] => 212060063) Array ( [id] => 1 [rating] => 3 [password] => 1988 [username] => 212060061)
So no difference can be found in the way of SORT.
At your last code line, replace:
print_r($newarray);
by:
foreach($newarray as $child_array)
{
print_r($child_array);
}
Upvotes: 2
Reputation: 4097
You need separate arrays correct? This code will create single arrays:
include 'connect.php';
$query = mysql_query("SELECT * FROM users");
$x = 0;
while($data = mysql_fetch_assoc($query)){
$variable_name = 'record'.$x;
$$variable_name = $data;
$x++;
}
print_r($record0);
print_r($record1);
//etc...
The double $$ on the first run will be $record0
Upvotes: 0
Reputation: 91942
The easiest thing would probably be to sort them directly in SQL:
SELECT * FROM users ORDER BY id
I personally try to avoid struggling with array_multisort as much as I can since it's hard to work with. I have yet to see a problem where it is easier to _multisort than usort or letting the database layer handle it.
Upvotes: 2