Sort PHP Multidimensional Array by Sub-Value

PHP Sort Array By SubArray Value

Use usort.

function cmp_by_optionNumber($a, $b) {
return $a["optionNumber"] - $b["optionNumber"];
}

...

usort($array, "cmp_by_optionNumber");

In PHP ≥5.3, you should use an anonymous function instead:

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

Note that both code above assume $a['optionNumber'] is an integer. Use @St. John Johnson's solution if they are strings.


In PHP ≥7.0, use the spaceship operator <=> instead of subtraction to prevent overflow/truncation problems.

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

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'];
});

With PHP 7 you can use the spaceship operator:

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

Finally, in PHP 7.4 you can clean up a bit with an arrow function:

usort($myArray, fn($a, $b) => $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.

How to sort array based on subarray value in PHP

Try -

usort($array, function($a, $b) {
return $a['Product']['name'] > $b['Product']['name'];
});

usort()

PHP sort subarray value?

Simple enough:

foreach($arr as &$v) {
sort($v);
}

The &$v allow the values to be iterated over by reference, allowing modification inside the foreach loop (and thus sorting of each subarray).

PHP sort array alphabetically using a subarray value

Here is your answer and it works 100%, I've tested it.

<?php
$a = Array(
1 => Array(
'name' => 'Peter',
'age' => 17
),
0 => Array(
'name' => 'Nina',
'age' => 21
),
2 => Array(
'name' => 'Bill',
'age' => 15
),
);
function compareByName($a, $b) {
return strcmp($a["name"], $b["name"]);
}
usort($a, 'compareByName');
/* The next line is used for debugging, comment or delete it after testing */
print_r($a);

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 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);

PHP - Sort Array By SubArray Value By Maintaining original Array key

$arr being your array:

//obtain list of values to sort by
foreach ($arr as $id => $value) {
$names[$id] = $value['name'];
}
$keys = array_keys($arr);
array_multisort(
$names, SORT_ASC, SORT_NUMERIC, $arr, $keys
);
$result = array_combine($keys, $arr);

You were probably missing the last step combining the array with given keys.

Trying to sort a multidimensional array, by a sub-value with special characters

Below is a possible fix based on your initial approach:

usort($countries, function($a, $b) {
$collator = collator_create('en');
$arr = array($a['name'], $b['name']);
collator_asort($collator, $arr, Collator::SORT_STRING);

return array_pop($arr) == $a['name'];
});

For optimal performance on long lists, you may want to instantiate $collator only once outside the scope of the anonymous function, though.

php sort multidimensional array that is multiple layers deep by sub key value

Because you have second-level keys which vary for each element in the array, you need to use array_values inside the comparison function to re-index those arrays and allow access to the C0 value for each element. You also need to use uasort to retain your associative keys:

uasort($tmp_array_value, function ($a, $b) {
return array_values($a)[0]['C0'] - array_values($b)[0]['C0'];
});

print_r($tmp_array_value);

Note that since the second level arrays only have one element, you could (as pointed out by @Kevin) also use

return reset($a)['C0'] - reset($b)['C0'];

to get the 'CO' value from the first array element.

Output:

Array
(
[c911f95676eb7e5979fda3770bff1a03] => Array
(
[022218] => Array
(
[C0] => 1
[C1] => 1
[C2] => 1
[C3] => 1
)
)
[9e5dae29ec5a83d503f2e4d4b5f29f91] => Array
(
[007Hal007] => Array
(
[C0] => 2
[C1] => 1
[C2] => 1
)
)
[9317264ea7cc25c6f4f92bbdeb01ec63] => Array
(
[011210] => Array
(
[C0] => 3
[C1] => 1
[C2] => 1
[C3] => 1
)
)
)

Demo on 3v4l.org



Related Topics



Leave a reply



Submit