1

I am trying to reorder an associative array which has label value pairs.I am getting the label data aligned in alphabetical order.but i need the array to be ordered in the form of array of months. My input looks like this:

Array
(
    [0] => Array
        (
            [label] => August
            [value] => 100.55
        )

    [1] => Array
        (
            [label] => November
            [value] => 100.24
        )

    [2] => Array
        (
            [label] => October
            [value] => 99.19
        )

    [3] => Array
        (
            [label] => September
            [value] => 100.11
        )

)

output: In the output array i need the arrays to be ordered as August,September,October,November.Any suggestions please

Manasa
  • 189
  • 3
  • 16
  • 1
    @u_mulder, this is not a duplicate of the question you've provided. – Taha Paksu Nov 28 '17 at 06:53
  • You can use array_shift(). Please check the answer on this link https://stackoverflow.com/questions/15936309/php-rearrange-array-by-specific-index – Rasko Nov 28 '17 at 06:56
  • 1
    Let suppose your array is $original_array.Then use this code $val){ if($key=array_search($val['label'], $months)){ $result[$key]=array('label'=>$val['label'],'value'=>$val['value']); } } ksort($result); print_r($result); ?> – Confused Nov 28 '17 at 07:04
  • @u_mulder this question is not a duplicate, please re-open – evilReiko Nov 28 '17 at 07:43
  • __NO__, I will not reopen this question. And yes, it is a __duplicate__. OP shows no effort in researching the task, so the dupe is a point to start with. – u_mulder Nov 28 '17 at 07:46
  • @u_mulder If you insist, it should be a duplicate of this: [How to sort an array of associative arrays by value of a given key in PHP?](https://stackoverflow.com/q/1597736/134824]) – evilReiko Nov 28 '17 at 07:52
  • This is just another duplicate. __EVERYTHING__ is described in a previous dupe. Case closed. – u_mulder Nov 28 '17 at 07:58
  • I'll add to the pile: https://stackoverflow.com/questions/5697759/array-sort-function-in-php/5697788#5697788 – mickmackusa Nov 29 '17 at 12:30

1 Answers1

2

Make order array and then sort by usort functon

$seq = array_flip([ 'August', 'September', 'October',  'November' ]);

usort($arr, function ($i1, $i2) use($seq) { return $seq[$i1['label']] - $seq[$i2['label']]; });  

demo

splash58
  • 26,043
  • 3
  • 22
  • 34