Tejas Khutale Tejas Khutale - 2 months ago 6
PHP Question

Change array order according to value in php

I have an array and I want change it's order.
Following is an my array

[1] => Array
(
[A] => 25/02/2016
[B] => ZPO
[C] => 2
[D] => 3
)
[2] =>array (
[A] => 25/02/2016
[B] => RTN
[C] => 2
[D] => 3
)
[3] =>array (
[A] => 25/02/2016
[B] => ZPO
[C] => 2
[D] => 3
)


index [2] array should be come at last because it contains value [B] = RTN
Means I want to reorder this array if [B] index contains value RTN should be come at last

Final output should be

[1] => Array
(
[A] => 25/02/2016
[B] => ZPO
[C] => 2
[D] => 3
)
[2] =>array (
[A] => 25/02/2016
[B] => ZPO
[C] => 2
[D] => 3
)
[3] =>array (
[A] => 25/02/2016
[B] => RTN
[C] => 2
[D] => 3
)

Answer

Is this what your looking for?

foreach ($aArrayToSort as $iPos => $aArray) {
    if($aArray['B'] == 'RTN'){
        $aArrayToSort[] = $aArrayToSort[$iPos];
        unset($aArrayToSort[$iPos]);
    }
}
Comments