Welcome to WebmasterWorld Guest from 3.80.6.254

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Array merge and sort value

     
4:44 pm on Nov 6, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 29, 2003
posts: 94
votes: 0


Hope some one can help

I have 2 arrays that i have merged using array_merge, the contents of the arrays are in alphabetical order by the [name] field, when i merge the arrays i need to sort the new merged array too but cant seem to do it.

example:

array 1 [aid]=>4654 [name]=>green [loc]=>apple
array 1 [aid]=>7836 [name]=>yellow [loc]=>banana
array 1 [aid]=>2469 [name]=>red [loc]=>tomato

array 2 [aid]=>6711 [name]=>brown [loc]=>mud
array 2 [aid]=>1744 [name]=>pink [loc]=>flower
array 2 [aid]=>9328 [name]=>white [loc]=>snow

when merged i get

newarray [aid]=>4654 [name]=>green [loc]=>apple
newarray [aid]=>7836 [name]=>yellow [loc]=>banana
newarray [aid]=>2469 [name]=>red [loc]=>tomato
newarray [aid]=>6711 [name]=>brown [loc]=>mud
newarray [aid]=>1744 [name]=>pink [loc]=>flower
newarray [aid]=>9328 [name]=>white [loc]=>snow

where i need

newarray [aid]=>6711 [name]=>brown [loc]=>mud
newarray [aid]=>4654 [name]=>green [loc]=>apple
newarray [aid]=>1744 [name]=>pink [loc]=>flower
newarray [aid]=>2469 [name]=>red [loc]=>tomato
newarray [aid]=>9328 [name]=>white [loc]=>snow
newarray [aid]=>7836 [name]=>yellow [loc]=>banana

sorry if i dont make it clear

i have tried

function multi_sort($array, $key)
{
$cmp_val="((\$a['$key']>\$b['$key'])?1:
((\$a['$key']==\$b['$key'])?0:-1))";
$cmp=create_function('$a, $b', "return $body;");
uasort($array, $cmp);
return $array;
}

multi_sort($mymergedarray, "name");

which i found on a search but this doesnt seem to work either

Thanks for any help

8:12 pm on Nov 6, 2008 (gmt 0)

Full Member

10+ Year Member

joined:July 5, 2004
posts:303
votes: 0


Assuming that your arrays are structured like below:
$array1[0]['aid']= '4654';
$array1[0]['name']= 'green';
$array1[0]['loc']= 'apple';
$array1[1]['aid']= '7836';
$array1[1]['name']= 'yellow';
$array1[1]['loc']= 'banana';
$array1[2]['aid']= '2469';
$array1[2]['name']= 'red';
$array1[2]['loc'] = 'tomato';

$array2[0]['aid']='6711';
$array2[0]['name']= 'brown';
$array2[0]['loc']= 'mud';
$array2[1]['aid']= '1744';
$array2[1]['name']= 'pink';
$array2[1]['loc']= 'flower';
$array2[2]['aid']= '9328';
$array2[2]['name']= 'white';
$array2[2]['loc'] = 'snow';

you should be able to just use PHP's built in array_multisort function

foreach ($array as $key) {
$aid[] = $key["aid"];
$name[] = $key["name"];
$loc[] = $key["loc"];
}
array_multisort($name, SORT_ASC, SORT_STRING, $aid, SORT_ASC, SORT_STRING, $loc, SORT_ASC, SORT_STRING, $array);
print_r($array);

9:24 pm on Nov 6, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 29, 2003
posts: 94
votes: 0


Thanks, tried that and got

Cannot use object of type stdClass as array

I think ill have to try something else to tackle this, thanks

10:11 pm on Nov 6, 2008 (gmt 0)

Full Member

10+ Year Member

joined:July 5, 2004
posts:303
votes: 0


Are you using an array, or an object? are you are pulling your data in from a database? If so what fetch method are you using?
could you post some of your actual code?
1:02 am on Nov 7, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 29, 2003
posts:94
votes: 0


Its a clients code, its an object fetched with SOAP, will have a good look through it and see what i can do

Thanks