...

# multi dimensional array sort

homerUK
11-22-2007, 11:22 AM
Hi guys,

I'm now having sorting the MD array...

\$arr [1][1.01][R11101][is_high_level] = 0;
\$arr [1][0.00][R11100][is_high_level] = 0;
\$arr [1][1.02][R11102][is_high_level] = 1;
\$arr [1][1.03][R11103][is_high_level] = 0;
\$arr [2][2.01][R21101][is_high_level] = 1;
\$arr [2][2.02][R21102][is_high_level] = 0;

I have the following to sort it

array_multisort(\$metric_array[3], SORT_DESC, SORT_NUMERIC);

yet it still doesnt do anything! I expect the element at the end, (is_high_level) where it's 1 to be at the top??

ALSO......

What I need to do is to set all the "is_high_level" elements = 0 where the first array element is X.

eg: x=1 would go through and set all the is_high_level elements to 0. I can then update the indiviual array element later.

should I just loop through and use a where??

homerUK
11-28-2007, 03:36 PM
still not having any luck - anyone able to help?

Fumigator
11-28-2007, 05:14 PM
Here's how I sort a multi-dimensional array:

//Make a single-index array based on the index from the multi-array you want to sort on
foreach (\$arr as \$key => \$val) {
\$sortSingle[\$key] = \$arr[\$key]['sortIndex'];
}

//Sort the single-index array
asort (\$sortSingle);

//Build new multi-array based on original multi-array in the order of the single-index array
reset (\$sortSingle);
while (list (\$singleKey, \$singleVal) = each (\$sortSingle)) {
\$newArr[] = \$arr[\$singleKey];
}

//Assign the new array to the original array
\$arr = \$newArr;

homerUK
11-28-2007, 06:19 PM
I tried this code from your example:

\$arr [1]["1.01"]["R11101"]["is_high_level"] = 0;
\$arr [1]["0.00"]["R11100"]["is_high_level"] = 0;
\$arr [1]["1.02"]["R11102"]["is_high_level"] = 1;
\$arr [1]["1.03"]["R11103"]["is_high_level"] = 0;
\$arr [2]["2.01"]["R21101"]["is_high_level"] = 1;
\$arr [2]["2.02"]["R21102"]["is_high_level"] = 0;

foreach (\$arr as \$key => \$val) {
\$sortSingle[\$key] = \$arr[\$key]['is_high_level'];
}

//Sort the single-index array
asort (\$sortSingle);

//Build new multi-array based on original multi-array in the order of the single-index array
reset (\$sortSingle);
while (list (\$singleKey, \$singleVal) = each (\$sortSingle)) {
\$newArr[] = \$arr[\$singleKey];
}

//Assign the new array to the original array
\$arr = \$newArr;
echo "<pre>";
print_r(\$arr);
echo "</pre>";

which gave me

Array
(
[0] => Array
(
[2.01] => Array
(
[R21101] => Array
(
[is_high_level] => 1
)

)

[2.02] => Array
(
[R21102] => Array
(
[is_high_level] => 0
)

)

)

[1] => Array
(
[1.01] => Array
(
[R11101] => Array
(
[is_high_level] => 0
)

)

[0.00] => Array
(
[R11100] => Array
(
[is_high_level] => 0
)

)

[1.02] => Array
(
[R11102] => Array
(
[is_high_level] => 1
)

)

[1.03] => Array
(
[R11103] => Array
(
[is_high_level] => 0
)

)

)

)

which appears it's not working... any ideas?! The high_level where it's 1 needs to be the first element in the array..... trouble is, it THEN needs sorting on the decimal number like 1.01 then 2.01 etc.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum