I have array Like this
$array = [
[
'project_id' => 1,
'Client' => [
['Name' => 'Franco',
'Amount' => 1000.00,
],
['Name' => 'Allan',
'Amount' => 1000.00,
],
['Name' => 'Booby',
'Amount' => 1000.00,
]
],
],
];
I use array_multisort and array_map to sort it by Name in descending, I already tried usort but still not working.. Any Help will be Appreciated
array_multisort(
array_map(
static function ($element) {
return $element['Client']['Name'];
},
$array
),
SORT_DESC,
$array
);
return $array;
3
Answers
Here’s a solution using a function I found from php documentation:
Yields:
Your question is not clear. However from your try what I can understand is you are trying to sort the client array based on ‘Name’. So to achieve this please try the below logic with the same usort.
A one liner using
array_multisort()
andarray_column()
:Output:
If you have multiple elements in
$array
that have to be sorted by client, you can usearray_walk()
around the sort (it’s still a one liner):