1

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) 
5
  • 1
    Sorted by rating? ID? Username? Commented Jan 27, 2012 at 22:33
  • The output you need looks like a list, but the output you're getting is the specific representation of a list (you could loop over the output you're getting). What does the code that uses the result look like? Commented Jan 27, 2012 at 22:37
  • If you want to store them separately, than just make a variable for each array. If you want to store 4 arrays together, you must store them in another array, or in an object... I'm courious tho, how the rest of the code looks like. Commented Jan 27, 2012 at 22:40
  • The mysql extension is outdated and on its way to deprecation. New code should use mysqli or PDO, both of which have important advantages, such as support for prepared statements. Don't use SELECT * unless you're writing a DB administration program; select only the columns you need. Commented Jan 28, 2012 at 0:32
  • What's the problem? If you have an array of arrays and need to work with the inner arrays, just iterate over the outer array, or (in this case) process each result separately when fetched rather than adding them to an array and processing it later. Commented Jan 28, 2012 at 0:33

3 Answers 3

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

+1 for spurning array_multisort()! I know that it's sometimes more efficient than a usort solution, but in all but the simplest arrays array_multisort() just makes life harder as you beat your head against this all-powerful function's mysterious results. And the code is often less readable, as it's generally performing a fairly large logical lead at a stroke.
I know, however the data is already dumped from 4 different files. My file does not actually includes the query. Only the $row variable. The idea is that i have so many queries and i am trying to release the stress on the server.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.