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

8
  • 1
    @u_mulder, this is not a duplicate of the question you've provided. Commented Nov 28, 2017 at 6:53
  • You can use array_shift(). Please check the answer on this link stackoverflow.com/questions/15936309/… Commented Nov 28, 2017 at 6:56
  • 1
    Let suppose your array is $original_array.Then use this code <?php $months=array('January','February','March','April','May','June','July','August','September','October','November','December'); $result=array(); foreach($original_array as $key=>$val){ if($key=array_search($val['label'], $months)){ $result[$key]=array('label'=>$val['label'],'value'=>$val['value']); } } ksort($result); print_r($result); ?> Commented Nov 28, 2017 at 7:04
  • @u_mulder this question is not a duplicate, please re-open Commented Nov 28, 2017 at 7: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. Commented Nov 28, 2017 at 7:46

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.