0

Okay, im having a really hard time and tried many solutions but i cant get it to work. Gave up some hours now on this, and still nothing.

I have an array that looks like this: $data['dietcontent'][1][1][1]

$data['dietcontent'][1][1][1]
--------------------^Day^Meal^Line

This array, i would like to sort out for their MealTime, that exists on $data['dietcontent'][1][1][1].MealTime

The 3d array is in this format:

$data['dietcontent'][1][$mealNumber][$mealLine]['MealTime'].

$mealNumber can be from 1 to 7, $mealLine can be from 1 to 7

Okay this is quite hard to explain. The structure could be done alot better, but since im just working out from it, I cant do nothing about.

So instead of explaining, I would like to give you an example of how it looks like now, and how i wish it to look like.

Here's an example:

$data['dietcontent'][1][1][1]['MealTime'] // is 07:00
$data['dietcontent'][1][1][2]['MealTime'] // is 07:00
$data['dietcontent'][1][1][3]['MealTime'] // is 07:00
$data['dietcontent'][1][1][4]['MealTime'] // is 07:00
$data['dietcontent'][1][1][5]['MealTime'] // is 07:00
$data['dietcontent'][1][1][6]['MealTime'] // is 07:00
$data['dietcontent'][1][1][7]['MealTime'] // is 07:00
$data['dietcontent'][1][2][1]['MealTime'] // is 01:30
$data['dietcontent'][1][2][2]['MealTime'] // is 01:30
$data['dietcontent'][1][2][3]['MealTime'] // is 01:30
$data['dietcontent'][1][2][4]['MealTime'] // is 01:30
$data['dietcontent'][1][2][5]['MealTime'] // is 01:30
$data['dietcontent'][1][2][6]['MealTime'] // is 01:30
$data['dietcontent'][1][2][7]['MealTime'] // is 01:30

Is how it can look like. Now i would like to sort the $mealNumber (the second []) place in this array, after the MealTime. So it will look like this after:

$data['dietcontent'][1][1][1]['MealTime'] // is 01:30
$data['dietcontent'][1][1][2]['MealTime'] // is 01:30
$data['dietcontent'][1][1][3]['MealTime'] // is 01:30
$data['dietcontent'][1][1][4]['MealTime'] // is 01:30
$data['dietcontent'][1][1][5]['MealTime'] // is 01:30
$data['dietcontent'][1][1][6]['MealTime'] // is 01:30
$data['dietcontent'][1][1][7]['MealTime'] // is 01:30
$data['dietcontent'][1][2][1]['MealTime'] // is 07:00
$data['dietcontent'][1][2][2]['MealTime'] // is 07:00
$data['dietcontent'][1][2][3]['MealTime'] // is 07:00
$data['dietcontent'][1][2][4]['MealTime'] // is 07:00
$data['dietcontent'][1][2][5]['MealTime'] // is 07:00
$data['dietcontent'][1][2][6]['MealTime'] // is 07:00
$data['dietcontent'][1][2][7]['MealTime'] // is 07:00

How can i do this?! Your help is really appreciated!!

Karem
  • 17,615
  • 72
  • 178
  • 278

1 Answers1

0
foreach($data['dietcontent'] as &$day){
    //create temp sorting array
    $mealsArr = array();
    foreach($day as $meal){
        $i = 1;
        foreach($meal as $line){
            //put the lines into the temp array using meal time as key
            $mealsArr[$line['MealTime']][$i] = $line;
            $i++;
        }
    }        
    //sort the array by key (meal time!)
    ksort($mealsArr);
    //set up the new meals in the same format as before
    $newMeals = array();
    $i = 1;
    foreach($mealsArr as $lines){
        $newMeals[$i] = $lines;
        $i++;
    }
    //put back the new lines
    $day = $newMeals;
}

Edit: setting wrong keys

Savid
  • 311
  • 1
  • 2
  • 4
  • how can i put it all back to $data['dietcontent'] ? I tried $day[] = $newMeals and then outside the foreach $data['dietcontent'] = $day; but it does not work then. – Karem Dec 27 '11 at 01:15
  • the `&$day` on line 1 is passed by reference. Any changes to $day will apply back to the original array $data['dietcontent']. so `$day = $newMeals;` will change the original array. You can also do `foreach($data['dietcontent'] as $key=>$day){` then on last line `$data['dietcontent'][$key] = $newMeals;` – Savid Dec 27 '11 at 01:27
  • thank you. It starts to work. But not properly. When it sorts, e.g Meal 2, it has 7 lines (the third [] in the array). In my example array, 2 of them are filled and i wish them to be the first in the array indexes (so in this case i can reach them by [1], [2]), and then the empty ones can come after, if there are any. They are like this already before the sorting with your code. Because right now it makes 2 empty lines, before one which is filled, then 1 empty line, then a filled one and then 2 more empty lines. – Karem Dec 27 '11 at 01:36
  • not sure exactly what you mean but if you need to sort only part of the array you have to separate the array then sort part of it then join it back up. You'd have to post more examples for more help – Savid Dec 27 '11 at 01:57