Array (
[products] => Array (
[0] => Array (
[LOTid] => Array ( [0] => 20200901000001 )
[Brand] => SAMSUNG
[Product] => TV
[DisplayOrder] => 6 )
[1] => Array (
[LOTid] => Array ( [0] => 20200901000002 )
[Brand] => LG
[Product] => TV
[DisplayOrder] => 9 )
[2] => Array (
[LOTid] => Array ( [0] => 20200901000003 )
[Brand] => SAMSUNG
[Product] => MOBILE
[DisplayOrder] => 1 )
[3] => Array (
[LOTid] => Array ( [0] => 20200901000002 )
[Brand] => LG
[Product] => MOBILE
[DisplayOrder] => 4 )
)
[status] => ok
)
How to sort above multidimensional array using values of Brand and DisplayOrder keys? So sorted array will be arranged like following sample. Same Brand will display together and then records of same brand will be displayed in ascending order of DisplayOrder.
Array (
[products] => Array (
[0] => Array (
[LOTid] => Array ( [0] => 20200901000003 )
[Brand] => SAMSUNG
[Product] => MOBILE
[DisplayOrder] => 1 )
[1] => Array (
[LOTid] => Array ( [0] => 20200901000001 )
[Brand] => SAMSUNG
[Product] => TV
[DisplayOrder] => 6 )
[2] => Array (
[LOTid] => Array ( [0] => 20200901000002 )
[Brand] => LG
[Product] => MOBILE
[DisplayOrder] => 4 )
[3] => Array (
[LOTid] => Array ( [0] => 20200901000002 )
[Brand] => LG
[Product] => TV
[DisplayOrder] => 9 )
)
)
If sorting by values of 2 keys is too complex then ascending sorting on just DisplayOrder key will also work. So resulting array data will be displayed in order of 1, 4, 6, 9. That means Brand key will not be considered in this case.
Thank You,
EDIT
I tried following 2 codes taken from internet but both are giving illegal offset error. I tried below code to sort array only on DisplayOrder key.
#1
function method1($a,$b)
{
return ($a["products"]["DisplayOrder"] <= $b["products"]["DisplayOrder"]) ? -1 : 1;
}
usort($result, "method1");
#2
usort($result, function($a, $b) {
return $a['DisplayOrder'] <=> $b['DisplayOrder'];
});