How to Sort a Multidimensional Array in PHP

How to Sort a Multi-dimensional Array by Value

Try a usort. If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:

function sortByOrder($a, $b) {
return $a['order'] - $b['order'];
}

usort($myArray, 'sortByOrder');

Starting in PHP 5.3, you can use an anonymous function:

usort($myArray, function($a, $b) {
return $a['order'] - $b['order'];
});

And finally with PHP 7 you can use the spaceship operator:

usort($myArray, function($a, $b) {
return $a['order'] <=> $b['order'];
});

To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.

usort($myArray, function($a, $b) {
$retval = $a['order'] <=> $b['order'];
if ($retval == 0) {
$retval = $a['suborder'] <=> $b['suborder'];
if ($retval == 0) {
$retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder'];
}
}
return $retval;
});

If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual.

Sort php multidimensional array by sub-value

You can use the usort function.

function cmp($a, $b) {
return $a["mid"] - $b["mid"];
}
usort($arr, "cmp");

See it

Sort multidimensional array in PHP using foreach

array_multisort sorts the array in place, it does not return the sorted array. You need to use it like this:

foreach ($slice1 as $col) {
array_multisort($col);
$slicesort[] = $col;
}

Having said this, array_multisort is somewhat overkill here, and I'm not sure that you really need to create a copy of the array. This will do just fine:

foreach ($slice1 as &$col) {
sort($col);
}

This applies sort to each array within $slice1 by reference, and thereby orders $slice1 in place.

Sort PHP multi-dimensional array based on value in inner array?

Thinking,more useful and practical
http://php.net/manual/en/function.sort.php

function array_sort($array, $on, $order=SORT_ASC){

$new_array = array();
$sortable_array = array();

if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}

switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}

foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}

return $new_array;
}

How to use

 $list = array(
array( 'type' => 'suite', 'name'=>'A-Name'),
array( 'type' => 'suite', 'name'=>'C-Name'),
array( 'type' => 'suite', 'name'=>'B-Name')
);

$list = array_sort($list, 'name', SORT_ASC);

array(3) { [0]=> array(2) { ["type"]=> string(5) "suite" ["name"]=> string(6) "A-Name" } [2]=> array(2) { ["type"]=> string(5) "suite" ["name"]=> string(6) "B-Name" } [1]=> array(2) { ["type"]=> string(5) "suite" ["name"]=> string(6) "C-Name" } }

How do I sort a multidimensional array by one of the fields of the inner array in PHP?

You need to use usort, a function that sorts arrays via a user defined function. Something like:

function cmp($a, $b)
{
if ($a["price"] == $b["price"]) {
return 0;
}
return ($a["price"] < $b["price"]) ? -1 : 1;
}

usort($yourArray,"cmp")

PHP - How to sort multidimensional array by Key?

Working demo.

You can use array_multisort to achieve your requirement.

$keys = [];
foreach($arr as $k => $item){
$keys[] = key($item);
}
array_multisort($keys, SORT_NATURAL, $arr);

array_multisort — Sort multiple or multi-dimensional arrays

SORT_NATURAL - compare items as strings using "natural ordering" like natsort().

How do I Sort a Multidimensional Array in PHP

You can use array_multisort()

Try something like this:

foreach ($mdarray as $key => $row) {
// replace 0 with the field's index/key
$dates[$key] = $row[0];
}

array_multisort($dates, SORT_DESC, $mdarray);

For PHP >= 5.5.0 just extract the column to sort by. No need for the loop:

array_multisort(array_column($mdarray, 0), SORT_DESC, $mdarray);

Sort a multi-dimensional array by the size of its sub-arrays

You can achieve it by utilizing usort function.

function cmp($a, $b){
return (count($b) - count($a));
}
usort($array, 'cmp');
$highest_3_sub_arrays = array_slice($array, 0, 3);

How do I sort a multidimensional array with stdClass Objects by values of a key?

You're almost right, but $row[$col] tries to access the objects like an array. You want something like $row->{$col} instead. Here's a simpler, working example:

$db = array(
0 => (object) array('name' => 'Business3'),
1 => (object) array('name' => 'Business2'),
2 => (object) array('name' => 'Business1')
);

$col = 'name';
$sort = array();
foreach ($db as $i => $obj) {
$sort[$i] = $obj->{$col};
}

$sorted_db = array_multisort($sort, SORT_ASC, $db);

print_r($db);

Outputs:

Array
(
[0] => stdClass Object
(
[name] => Business1
)

[1] => stdClass Object
(
[name] => Business2
)

[2] => stdClass Object
(
[name] => Business3
)

)

How to sort a multidimensional array by month names in php

An approach, using usort() and the appropriate comparison function is an option:

<?php
$array = array(
array("netDe" => 22970.00, "netEran" => 71330.00, "compiled_month" => "May/2021"),
array("netDe" => 22970.00, "netEran" => 71330.00, "compiled_month" => "Jun/2021"),
array("netDe" => 65970.00, "netEran" => 28330.00, "compiled_month" => "Jul/2021"),
array("netDe" => 10396.00, "netEran" => 27324.00, "compiled_month" => "Jan/2021"),
array("netDe" => 20792.00, "netEran" => 54648.00, "compiled_month" => "Feb/2021" )
);

usort(
$array,
function ($a, $b) {
return
DateTime::createFromFormat('d/M/Y', "01/".$a["compiled_month"]) <=>
DateTime::createFromFormat('d/M/Y', "01/".$b["compiled_month"]);
}
);
?>


Related Topics



Leave a reply



Submit