Reputation: 45
This is the following array i am getting...! how could I sort it.
Array
(
[0] => Array
(
[0] => Array
(
[price] => 29.99
[params] =>
[text] => demotext
)
[1] => Array
(
[price] => 22.40
[params] =>
[text] => demotext
)
[2] => Array
(
[price] => 12.95
[params] =>
[text] => demotext
)
[3] => Array
(
[price] => 9.60
[params] =>
[text] => demotext
)
)
[1] => Array
(
[0] => Array
(
[price] => 8.16
[params] =>
[text] => demotext
)
[1] => Array
(
[price] => 7.66
[params] =>
[text] => demotext
)
[2] => Array
(
[price] => 7.19
[params] =>
[text] => demotext
)
[3] => Array
(
[price] => 7.14
[params] =>
[text] => demotext
)
)
As you can see in index 2 array is not sorted beacuse 5.10 should on index [1] and 4.79 on index [2]
[2] => Array
(
[0] => Array
(
[price] => 5.99
[params] =>
[text] => demotext
)
[1] => Array
(
[price] => 4.79
[params] =>
[text] => demotext
)
[2] => Array
(
[price] => 5.10
[params] =>
[text] => demotext
)
[3] => Array
(
[price] => 4.20
[params] =>
[text] => demotext
)
)
[3] => Array
(
[0] => Array
(
[price] => 4.08
[params] =>
[text] => demotext
)
[1] => Array
(
[price] => 4.00
[params] =>
[text] => demotext
)
[2] => Array
(
[price] => 3.20
[params] =>
[text] => demotext
)
[3] => Array
(
[price] => 3.19
[params] =>
[text] => demotext
)
)
[4] => Array
(
[0] => Array
(
[price] => 2.86
[params] =>
[text] => demotext
)
[1] => Array
(
[price] => 3.58
[params] =>
[text] => demotext
)
[2] => Array
(
[price] => 2.82
[params] =>
[text] => demotext
)
[3] => Array
(
[price] => 2.90
[params] =>
[text] => demotext
)
)
)
Upvotes: 0
Views: 1307
Reputation: 45
This what I sorted after great fuss , to break multi-dimension array into One Array and then sorted it according to price.
$array = your array;
// merging multi-dimension array into one array.
$result = array_merge_recursive($array[0],$array[1],$array[2],$array[3],$array[4]);
// now Sort array according to price
array_multisort($result, SORT_ASC);
foreach ($result as $key => $val) {
foreach ($val as $new) {
}
echo $new;
}
Upvotes: 0
Reputation: 6344
Suppose $arr is your array then try:
foreach($arr as &$ar){
foreach($ar as $key=>$r){
$price[$key] = $r['price'];
$params[$key] = $r['params'];
$text[$key] = $r['text'];
}
array_multisort($price, SORT_DESC, $params, SORT_REGULAR, $text,SORT_REGULAR,$ar);
}
See demo here
Upvotes: 1
Reputation: 622
I think you could use array_multisort() for that http://ch2.php.net/array_multisort
Upvotes: 1
Reputation: 9430
Try using array_multisort function https://www.php.net/array_multisort
Upvotes: 0