i got an array like this:
array
'list_10' =>
array
row_0 =>
array
'Id' => string '118579'
'Status' => string '3'
row_1 =>
array
'Id' => string '117662'
'Status' => string '2'
row_2 =>
array
'Id' => string '117662'
'Status' => string '2'
'list_11' =>
array
row_0 =>
array
'Id' => string '112564'
'Status' => string '2'
row_1 =>
array
'Id' => string '153622'
'Status' => string '3'
row_2 =>
array
'Id' => string '112832'
'Status' => string '1'
i want to "natsort" the first key "list_XX", making it start with 0,1,2,.. instead of 10,11,12,13,0,1,2,3,...
i played around with array_multisort but i cant seem to set the right params to make it do what i want, if its even capable of doing this.
any advice?
Assuming your array is something like this:
$array = [
'list_11' =>
[
'row_0' =>
[
'Id' => '118579',
'Status' => '3'
],
'row_1' =>
[
'Id' => '117662',
'Status' => '2'
],
'row_2' =>
[
'Id' => '117662',
'Status' => '2'
]
],
'list_10' =>
[
'row_0' =>
[
'Id' => '112564',
'Status' => '2'
],
'row_1' =>
[
'Id' => '153622',
'Status' => '3'
],
'row_2' =>
[
'Id' => '112832',
'Status' => '1'
]
],
'list_1' =>
[
'row_0' =>
[
'Id' => '32323232',
'Status' => '3'
],
'row_1' =>
[
'Id' => '2353333',
'Status' => '2'
],
'row_2' =>
[
'Id' => '117662',
'Status' => '2'
]
]
];
Using array_multisort :
$sort = [];
foreach($array as $el=>$val){
$sort[] = $el;
}
array_multisort($array,SORT_NUMERIC,$sort,SORT_NATURAL);
var_dump($array);
Will print :
array(3) {
["list_1"]=>
array(3) {
["row_0"]=>
array(2) {
["Id"]=>
string(8) "32323232"
["Status"]=>
string(1) "3"
}
["row_1"]=>
array(2) {
["Id"]=>
string(7) "2353333"
["Status"]=>
string(1) "2"
}
["row_2"]=>
array(2) {
["Id"]=>
string(6) "117662"
["Status"]=>
string(1) "2"
}
}
["list_10"]=>
array(3) {
["row_0"]=>
array(2) {
["Id"]=>
string(6) "112564"
["Status"]=>
string(1) "2"
}
["row_1"]=>
array(2) {
["Id"]=>
string(6) "153622"
["Status"]=>
string(1) "3"
}
["row_2"]=>
array(2) {
["Id"]=>
string(6) "112832"
["Status"]=>
string(1) "1"
}
}
["list_11"]=>
array(3) {
["row_0"]=>
array(2) {
["Id"]=>
string(6) "118579"
["Status"]=>
string(1) "3"
}
["row_1"]=>
array(2) {
["Id"]=>
string(6) "117662"
["Status"]=>
string(1) "2"
}
["row_2"]=>
array(2) {
["Id"]=>
string(6) "117662"
["Status"]=>
string(1) "2"
}
}
}