好的,我很难过并尝试了很多解决方案,但我无法让它发挥作用。现在给这个放了几个小时,但仍然没有。
我有一个如下所示的数组:$data['dietcontent'][1][1][1]
$data['dietcontent'][1][1][1]
--------------------^Day^Meal^Line
这个数组,我想为他们的MealTime排序,它存在于$data['dietcontent'][1][1][1].MealTime
3d数组采用以下格式:
$data['dietcontent'][1][$mealNumber][$mealLine]['MealTime'].
$ mealNumber可以是1到7, $ mealLine可以是1到7
好的,这很难解释。结构可以做得更好,但是因为我只是从中解决了,所以我什么都不做。
所以我不想解释,而是想给你一个现在看起来如何的例子,以及我希望它看起来如何。
以下是一个例子:
$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
它是怎么样的。现在我想在MealTime之后对这个数组中的$ mealNumber(第二个[])位置进行排序。所以它会在这之后看起来像这样:
$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
我该怎么做?!非常感谢您的帮助!!
答案 0 :(得分: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;
}
修改:设置错误的密钥