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

}

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.