5

this is the array i like to sort in a particular order

$aData = Array
  (
    [break] => Array
        (
            [Indoor room] => 42
            [Gym Class] => 19
        )
    [finish] => Array
        (
            [Indoor room] => 42
            [Gym Class] => 19
        )

    [lunch] => Array
        (
            [Indoor room] => 7
        )

    [period1] => Array
        (
            [Indoor room] => 12
            [Gym Class] => 22
        )

    [period2] => Array
        (
            [Gym Class] => 14
            [Indoor room] => 25
        )

    [period3] => Array
        (
            [Gym Class] => 21
            [Indoor room] => 11
        )

    [period4] => Array
        (
            [Gym Class] => 22
            [Indoor room] => 20
        )

    [period5] => Array
        (
            [Gym Class] => 16
            [Indoor room] => 9
        )

)

But i like it in this order:

break, period1, period2, lunch, period3, period5, period6, finish

for this I am trying the following php code

$arraySort = [
  "break",  
  "period1", 
  "period2", 
  "period3", 
  "lunch",
  "period4",
  "period5", 
  "period6", 
  "finish" 
];

   foreach($aData as $period => $catsScore){
  echo 'test '.$period.'<br/>';  
  $periodItem = [$period];
  foreach($arraySort as $cat){
      echo 'new: '.$cat.'<br/>';
      $periodItem[] = $catsScore;
  }
  $output[] = $periodItem;
    }


print_r($output);

4 Answers 4

6

Easy- Just use the arraySort as the assoc key and get the corresponding array / value from the original array,

<?php

$arraySort = [
  "break",  
  "period1", 
  "period2", 
  "period3", 
  "lunch",
  "period4",
  "period5", 
  "period6", 
  "finish" 
];

$final_array = [];

foreach($arraySort  as $arraySo){
  $final_array[$arraySo] = isset($aData[$arraySo]) ? $aData[$arraySo] : [];
}

print_r($final_array);

Output:- https://3v4l.org/4LXvS

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

1 Comment

Thanks Alive, this is the one I understand. You just use the arraySort as the assoc key and get the corresponding array / value from the original array, right?
3

Make correctly ordered array and fill it by value from source array

$final_array = array_replace(array_fill_keys($arraySort, []), $aData);

demo

Comments

1

Alternatively you could use an actual sorting function:

uksort(
    $aData,
    function($a,$b)use($arraySort){
        return array_search($a, $arraySort) - array_search($b, $arraySort);
    }
);

Comments

-1

you can use array_combine for this purpose:

$arrary_sort = ["break", "period1"];
$final_array = array_combine($array_sort, $your_array_here);

2 Comments

That will just "rename" the existing values, not reorder them correctly
You change keys but don't reorder array

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.