Jump to content

Sort multi-dimensional array


fxuser

Recommended Posts

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

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.