fxuser Posted December 30, 2010 Share Posted December 30, 2010 Hello , i am trying to sort a 2d dimensional array by the first column(id) and i havent find anything till now This is my code for the 2d array: $username = $_SESSION['username']; $c = 0; $row=1; $col=1; $users_array = array(); $get_from = mysql_query("SELECT id FROM my_activity WHERE from_user='$username'"); while($fetch1 = mysql_fetch_array($get_from)){ $id[$c] = $fetch1[0]; $get_from_user = mysql_query("SELECT * FROM my_activity WHERE id='$id[$c]'"); $get_user_rows = mysql_num_rows($get_from_user); while($guser = mysql_fetch_array($get_from_user)){ $user[$c] = $guser['to_user']; $users_array[$id[$c]][$user[$c]]= $id[$c].".".$user[$c]; $c++; } } $get_to = mysql_query("SELECT * FROM my_activity WHERE to_user='$username'"); while($fetch2 = mysql_fetch_array($get_to)){ $id[$c] = $fetch2[0]; $get_to_user = mysql_query("SELECT from_user FROM my_activity WHERE id='$id[$c]'"); while($tuser = mysql_fetch_array($get_to_user)){ $user[$c] = $tuser[0]; $users_array[$id[$c]][$user[$c]]= $id[$c].".".$user[$c]; $c++; } } This is what i get if i simply loop the array to get the result: Array ( [6] => Array ( [checkdate] => 6.checkdate ) [25] => Array ( [asdsadsad] => 25.asdsadsad ) [7] => Array ( [webuser] => 7.webuser ) [9] => Array ( [newuser] => 9.newuser ) [10] => Array ( [bot77un] => 10.bot77un ) ) I am trying to sort it by the ids 6,25,7,9,10 in DESC Quote Link to comment Share on other sites More sharing options...
johnny86 Posted December 30, 2010 Share Posted December 30, 2010 ksort($array); will sort your array comparing keys. If you want to do desc you can do array_reverse($array) for the sorted array. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 30, 2010 Share Posted December 30, 2010 That code really needs some work. You should be sorting your records when you query the database, then you don't need to manipulate the results. You should also be using JOINs instead of running queries in loops. In fact, I'm really confused by your code. You do a query to get the ids from a table where the from_user is a specific value THEN you do queries on each ID to get more data. Why don't you get all the data you need on the first query??? You can get everythign you need with one query in the order you want it. $username = $_SESSION['username']; $query = "SELECT id, from_user, to_user FROM my_activity WHERE from_user='$username' OR to_user='$username' ORDER BY id"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { echo "ID: {$row['id']}, From: {$row['from_user']}, To{$row['to_user']}<br />\n"; } Quote Link to comment Share on other sites More sharing options...
fxuser Posted December 31, 2010 Author Share Posted December 31, 2010 thanks both , seems i have got it to work. If anything comes up regarding this problem ill reply back soon. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.