我有一个名为$ array的如下。我需要能够根据[day]值拉出其中的特定数组。
因此,例如,我可以将$ day2等于$ array [1],因为它的日值是2,依此类推。我怎么能这样做呢?当它们进来时它们没有被排序,并且不能有倍数,因为$ array中的最大数组数量可以是7,因为一周有7天。
Array
(
[0] => Array
(
[time_id] => 1
[user_id] => 4
[day] => 1
[12a] => 0
[1a] => 0
[2a] => 0
[3a] => 0
[4a] => 0
[5a] => 0
[6a] => 0
[7a] => 0
[8a] => 0
[9a] => 0
[10a] => 0
[11a] => 0
[12p] => 1
[1p] => 1
[2p] => 1
[3p] => 1
[4p] => 1
[5p] => 1
[6p] => 1
[7p] => 1
[8p] => 0
[9p] => 0
[10p] => 0
[11p] => 0
)
[1] => Array
(
[time_id] => 2
[user_id] => 4
[day] => 2
[12a] => 1
[1a] => 1
[2a] => 1
[3a] => 1
[4a] => 1
[5a] => 1
[6a] => 1
[7a] => 1
[8a] => 1
[9a] => 1
[10a] => 1
[11a] => 1
[12p] => 1
[1p] => 1
[2p] => 1
[3p] => 1
[4p] => 1
[5p] => 1
[6p] => 1
[7p] => 1
[8p] => 1
[9p] => 0
[10p] => 1
[11p] => 1
)
)
为任何感兴趣的人编辑:
这是我最终得到的结果...... $ settings是原始的多阵列...
//set counter
$counter = 0;
//loop through and set different day arrays
foreach($settings as $value) {
// inc counter
$counter++;
//set array using variable variables ($hourstemp is actually $hours1, $hours2, $hours3, etc.)
if($value['day'] == $counter) {
$hourstemp = 'hours'.$counter;
$$hourstemp = $value;
}
}
答案 0 :(得分:1)
如果您想获得特定日期:
$day = 2;
foreach($array as $value) {
if($value['day'] == $day) {
$result = $value;
break;
}
}
或者如果它们是一致的,那个'day'总是比索引大1,那么只需:
$result = $array[$day-1];
或者如果你想重新排序数组,那么索引就是'day':
foreach($array as $value) {
$result[$value['day']] = $value;
}
//then you can just use $result[$day]