0

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
                    )    
Tejas Khutale
  • 84
  • 2
  • 19

2 Answers2

2

You can use usort and provide a function:

usort(
  $array,
  // compare function for value 'B'.
  function($arr1, $arr2) { 
      // descending order ($arr2, $arr1). for ascending compare ($arr1, $arr2)
      return strcmp($arr2['B'], $arr1['B']);
  });
somnium
  • 1,487
  • 9
  • 15
2

Is this what your looking for?

foreach ($aArrayToSort as $iPos => $aArray) {
    if($aArray['B'] == 'RTN'){
        $aArrayToSort[] = $aArrayToSort[$iPos];
        unset($aArrayToSort[$iPos]);
    }
}
atoms
  • 2,993
  • 2
  • 22
  • 43
  • I want that RTN records. I dont want to unset that – Tejas Khutale Sep 29 '16 at 10:34
  • 1
    I'm adding it to the end of the array then removing it. Saved storing it in a temp var. UPDATE - noticed an error in code, see what your saying now.. Updated accordingly – atoms Sep 29 '16 at 10:34