How to Sort a Multidimensional Array by a Certain Key

Sort multi-dimensional array by specific key

You would use usort() - http://php.net/usort

My suggestion would be:

    function cmp($a, $b)
{
return strcmp($a->display_name, $b->display_name);
}

usort($blogusers, "cmp");

foreach ($blogusers as $bloguser)
{
...

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 to sort a multidimensional array by a certain key?


//define a comparison function
function cmp($a, $b) {
if ($a['status'] == $b['status']) {
return 0;
}
return ($a['status'] < $b['status']) ? -1 : 1;
}

usort($array, "cmp");

That should do what you want, you can alter the comparison function to sort on whatever key you want.

php sort multidimensional array by specific key

Use this:

foreach($myArray as $emailAddressKey=>$datesArray){
krsort($myArray[$emailAddressKey]);
}
print_r($myArray);

or (but i prefer the first option)

foreach($myArray as &$value){
krsort($value);
// this works only if $value is passed by reference. If it's not,
// it will update $value, but not $myArray[$key] as $value is only
// a local variable.
}
print_r($myArray);

This is the sorting method:

krsort — Sort an array by key in reverse order

bool krsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

See a working example here: https://3v4l.org/pok2e

PHP - sort multidimensional array by key

You can shorten your loop with:

ksort($array);
foreach($array as &$value) {
ksort($value);
}

Or use array_walk:

ksort($array);
array_walk($array, 'ksort');

Naturally sort a multi-dimensional array by key

Something simpler. Extract the array keys and sort those, sorting the original by that:

array_multisort(array_keys($array), SORT_NATURAL, $array);

With case insensitivity:

array_multisort(array_keys($array), SORT_NATURAL | SORT_FLAG_CASE, $array);

Sort multidimensional array by specific key value

Try with this:

<?php 


$arr = array();

$arr["2022-02-28"] = [
array("post_id"=>"3656", "time"=>"16:05", "priority"=>"0"),
array("post_id"=>"4856", "time"=>"13:05", "priority"=>"3")];
$arr["2022-03-01"] = [
array("post_id"=>"3656", "time"=>"16:05", "priority"=>"0"),
array("post_id"=>"3636", "time"=>"13:05", "priority"=>"1")
];



foreach($arr as $key => $value){

usort($value, function($a,$b){
return strtotime($a["time"])>strtotime($b["time"]);
});

$arr[$key] = $value;

}

echo "<pre>";
var_dump($arr);
echo "</pre>";

Output:

array(2) {
["2022-02-28"]=>
array(2) {
[0]=>
array(3) {
["post_id"]=>
string(4) "4856"
["time"]=>
string(5) "13:05"
["priority"]=>
string(1) "3"
}
[1]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
}
["2022-03-01"]=>
array(2) {
[0]=>
array(3) {
["post_id"]=>
string(4) "3636"
["time"]=>
string(5) "13:05"
["priority"]=>
string(1) "1"
}
[1]=>
array(3) {
["post_id"]=>
string(4) "3656"
["time"]=>
string(5) "16:05"
["priority"]=>
string(1) "0"
}
}

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

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


Related Topics



Leave a reply



Submit