Jump to content

Recommended Posts

basically I have an array that looks something like this:

$myarray = array();
$myarray[0] = array("5", "3", "6", "1", "3");
$myarray[1] = array("1", "8", "4", "2", "0");
$myarray[2] = array("9", "1", "0", "0", "5");
$myarray[3] = array("8", "7", "1", "5", "6");
$myarray[4] = array("0", "2", "4", "7", "9");
$myarray[5] = array("5", "5", "5", "7", "5");

 

I would like to sort the array so I can print out the data in a table, but I want to sort the array by the first value (0) in the inner array (so it would print out in the order 2,3,0,5,1,4). Can anyone help me figure this out? (In the long run I would like to order by 0,1,3 for when values are the same but just ordering by 0 will work for me for now).

Hi,

 

with array_multisort() e.g. like this:

 

       

        $myarray = array();
        $myarray[0] = array("5", "3", "6", "1", "3");
        $myarray[1] = array("1", "8", "4", "2", "0");
        $myarray[2] = array("9", "1", "0", "0", "5");
        $myarray[3] = array("8", "7", "1", "5", "6");
        $myarray[4] = array("0", "2", "4", "7", "9");
        $myarray[5] = array("5", "5", "5", "7", "5");

        $myarray_sort = array();
        foreach ($myarray as $arr)
        {
            $myarray_sort[] = $arr[0];
        }

        array_multisort($myarray, $myarray_sort);

        print_r($myarray);

 

 

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.