Order Multidimensional Array Recursively at Each Level in PHP

Order multidimensional array recursively at each level in PHP

Use a recursive function:

// Note this method returns a boolean and not the array
function recur_ksort(&$array) {
foreach ($array as &$value) {
if (is_array($value)) recur_ksort($value);
}
return ksort($array);
}

Alphabetically sort multidimensional array's keys and values recursively in PHP

you need to check keys are numeric or alphabetic. try below solution you may need to modify conditions for your purpose:

<?php

function isAssoc(array $arr)
{
return array_keys($arr) !== range(0, count($arr) - 1);
}

function sortArray(&$arr){
if(isAssoc($arr)){
ksort($arr);
} else{
asort($arr);
}
foreach ($arr as &$a){
if(is_array($a)){
sortArray($a);
}
}
}

$unsorted = array(
'D' => array(
'C' => array('c', 'b', 'a'),
'B' => 'bvalue',
'A' => array('a', 'c', 'b'),
),
'C' => 'cvalue',
'B' => 'bvalue',
'A' => array(
'Z' => 'zvalue',
'A' => 'avalue',
'B' => 'bvalue',
)
);
sortArray($unsorted);

print_r($unsorted);

Output

Array
(
[A] => Array
(
[A] => avalue
[B] => bvalue
[Z] => zvalue
)

[B] => bvalue
[C] => cvalue
[D] => Array
(
[A] => Array
(
[0] => a
[2] => b
[1] => c
)

[B] => bvalue
[C] => Array
(
[2] => a
[1] => b
[0] => c
)

)

)

Sorting multi-dimensional array by random key

You need to sort the array by key value. So if you had an array

$year = ["7" => [], "9" => [], "3" => []];

you would go by

ksort($year); // ["3" => [], "7" => [], "9" => []]

See:
https://www.php.net/manual/en/function.ksort.php

And a running example:
http://sandbox.onlinephpfunctions.com/code/216a48077d871dbd871445013fc838ddb1130bd4

If you need to apply for multidimensional arrays, I suggest you use a recursive function like from this answer:
https://stackoverflow.com/a/4501406/5042856

// Note this method returns a boolean and not the array
function recur_ksort(&$array) {
foreach ($array as &$value) {
if (is_array($value)) recur_ksort($value);
}
return ksort($array);
}

Sort multidimensional array recursive by specific key

Check this out:

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.
(Picked from here: http://php.net/manual/en/control-structures.foreach.php)

You should try it with this one:

function alphaSort(&$a)
{
foreach ($a as &$oneJsonSite)
{
if (count($oneJsonSite["children"]) > 0) alphaSort($oneJsonSite["children"]);
}

usort($a, 'sortByAlpha');
}

How to sort all levels of multidimensional array by key?

function recursive_sort(&$arr) {
fs($arr);
foreach($arr as $k=> &$v){
if (isset($v['subs'])) {
recursive_sort($v['subs']);
}
}
}

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

function fs(&$array){

usort($array, 'sortByOrder');

}

After multiple tries I have this. It works. Name of key (subs) is hardcoded what is not so good, but... I am glad it works.

Is there a native multi-dimensional, multi-type sort in PHP?

array_multisort() can be used to sort several arrays at once, or a
multi-dimensional array by one or more dimensions.

This is basically how you sort different dimensions of your array:

$ar = array(
array("10", 11, 100, 100, "a"),
array( 1, 2, "2", 3, 1)
);
array_multisort($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);

Multi sort accepts variadic params. Read more here, please read scroll down and read the comments.


Or you can loop through your array's arrays and sort them one by one.

foreach ($matrix as &$row) { sort($row); } 

However, I think multi-sort would perform much better.

Recursive Difference of Arrays with Changed Array Order

You can achieve that by using array_diff, array_column and array_map:

$col = "projectId";
$res = array_diff(array_column($a, $col), array_column($b, $col));
$res = array_map(function($id) use ($col) {return [$col => $id];}, $res);

Reference: array-diff, array-column, array_map

Live example: 3v4l



Related Topics



Leave a reply



Submit