Jump to content

[SOLVED] sort multi dimentional array


nadeemshafi9

Recommended Posts

hello guys

 

i need to sort a multidimentional array both numericaly and alphabeticaly on seperate occasions, the sort function dosent seem to work the array looks like this.

 

$zone_array[1]["id"] = "1"

$zone_array[1]["name"] = "ablah"

$zone_array[1]["listitem"] = "<li>blah</li>"

 

$zone_array[2]["id"] = "2"

$zone_array[2]["name"] = "bblah"

$zone_array[2]["listitem"] = "<li>blah</li>"

 

$zone_array[3]["id"] = "3"

$zone_array[3]["name"] = "cblah"

$zone_array[3]["listitem"] = "<li>blah</li>"

 

$zone_array[4]["id"] = "4"

$zone_array[4]["name"] = "dblah"

$zone_array[4]["listitem"] = "<li>blah</li>"

 

 

the id's and names may be in different places as they are pulled out of the DB and may be in incorrect corelation with the numeric key.

 

how can i sort them alphabeticaly, i know how to sort but i have trouble referencing this type of array. i need to sort it by the id and then on another ocasion by the title.

Link to comment
https://forums.phpfreaks.com/topic/72294-solved-sort-multi-dimentional-array/
Share on other sites

function msort($array, $id) {

$temp_array = array();

while(count($array)>0) {

$lowest_id = 0;

$index=0;

foreach ($array as $item) {

if (isset($item[$id]) && $array[$lowest_id][$id]) {

if ($item[$id]<$array[$lowest_id][$id]) {

$lowest_id = $index;

}

}

$index++;

}

$temp_array[] = $array[$lowest_id];

$array = array_merge(array_slice($array, 0,$lowest_id), array_slice($array, $lowest_id+1));

}

return $temp_array;

}

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.