Jump to content

Ordering an array via an element with an array


Maxoplata

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);

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.