Forum Moderators: coopster

Message Too Old, No Replies

multisort unknown N levels multidimensional array

associative array sort with recursive function?

         

urbanzen

11:02 am on Feb 14, 2008 (gmt 0)

10+ Year Member



I'm having problem, or thinking way out of the boundary to do these kind of sorting...

What I 'm trying to do, is to sort an ecommerce's category with N levels, depending on how many subcategories there are..

I managed to sort the first level, but I still think my logic is way too unnecessary?


function sort_crap (&$array) {
foreach ($array as $key=>$val) {

$order_by[$key] = $val[0]['order_by'];
if ($val['subcategory'])
sort_crap($val['subcategory']);
}
array_multisort($order_by, SORT_ASC,SORT_NUMERIC, $array);
}
sort_crap($masterArray);

Here is an array for you to test


$masterArray = array (
100 =>
array (
0 =>
array (
'order' => '20',
),
'subcat' =>
array (
101 =>
array (
0 =>
array (
'order' => '0',
),
),
286 =>
array (
0 =>
array (
'order' => '1',
),
),
),
),
111 =>
array (
0 =>
array (
'order' => '10',
),
'subcat' =>
array (
112 =>
array (
0 =>
array (
'order' => '3',
),
),
115 =>
array (
0 =>
array (
'order' => '0',
),
'subcat' =>
array (
653 =>
array (
0 =>
array (
'order' => '0',
),
),
654 =>
array (
0 =>
array (
'order' => '0',
),
),
),
),
141 =>
array (
0 =>
array (
'order' => '0',
),
),
),
),
);

Thanks for the help in advance, everyone.

Urban

eelixduppy

12:51 am on Feb 26, 2008 (gmt 0)



Have you had any luck with finding a solution to this problem yet?

urbanzen

2:28 am on Feb 27, 2008 (gmt 0)

10+ Year Member



No I haven't. I had to break apart the arrays into N arrays, according to N levels. For example, if my array is 4 levels deep, I'll break it into 4 arrays, then sort each one, and rejoin those 4 arrays back into the original array to obtain my sorted list.

Kinda cpu intensive, but I don't have a better method to it =<

coopster

8:16 pm on Mar 1, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



A recursive sort seems like the way to go, but you haven't specified enough detail. Should keys and values be sorted, keys first, values second? Values only? Here is a smaller example:
$masterArray = array( 
'200' => array(
'sub3' => 3,
'sub1' => 1,
'sub2' => 2
),
'100' => array(
'sub6' => 6,
'sub5' => array(
'cat3' => 3,
'cat1' => 1,
'cat2' => 2
),
'sub4' => 4
),
'300' => array(
'sub7' => 7,
'sub8' => 8,
'sub9' => 9
)
);
function recursiveSort(&$array) {
/**
* Sort the array by it's keys first
*/
ksort($array);
foreach ($array as &$v) {
if (is_array($v)) {
recursiveSort($v);
}
}
/**
* Sort the array by it's values
*/
asort($array);
return $array;
}
recursiveSort($masterArray);
print_r($masterArray);

urbanzen

2:40 am on Mar 3, 2008 (gmt 0)

10+ Year Member



Thank you very much coopster,

So I guess there isn't an easier way to sort the message other than doing it resursively. I was hoping array_multisort can handle it.

By the way how can I organize my array and code similar to yours? even though I use the {code},{pre} tags, it's still quite un-organized

coopster

1:43 pm on Mar 3, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I use the "pre" tags with extra spaces. 2 spaces for every 1 space of indentation that you would like to see displayed. I use Eclipse for my editor and I have it set to use space indentation rather than tabs as my preference. So if I press the tab key it automatically inserts 4 spaces. It's a common coding standard.

urbanzen

2:51 am on Mar 4, 2008 (gmt 0)

10+ Year Member



Thank you Coopster. I prefer tab keys for our php. Seems that most of the php codes I have encountered are indented by tabs...

Thanks very much again for the tip!