循环遍历嵌套数组,测试并输出具有相同结构的结果

时间:2011-03-28 17:03:43

标签: php arrays

嗨,我想知道是否有人能指出我正确的方向,我只是不知道从哪里开始。

我有一个遵循这个结构的数组

Array ( 
    [0] => Array ( [day_id] => 1 [plan_id] => 3 [day_num] => 1 [schedule_time] => 1300866720 [checkin_time] => [fine] => ) 
    [1] => Array ( [day_id] => 2 [plan_id] => 3 [day_num] => 2 [schedule_time] => 1300953120 [checkin_time] => [fine] => ) 
    [2] => Array ( [day_id] => 3 [plan_id] => 3 [day_num] => 3 [schedule_time] => 1301039520 [checkin_time] => [fine] => ) 
    [3] => Array ( [day_id] => 4 [plan_id] => 3 [day_num] => 4 [schedule_time] => 1301125920 [checkin_time] => [fine] => ) )

我想要做的是循环测试'schedule_time'特定时间,如果匹配整个数组保存到新数组中。所以说数组0和1匹配它会像这样输出

Array ( 
        [0] => Array ( [day_id] => 1 [plan_id] => 3 [day_num] => 1 [schedule_time] => 1300866720 [checkin_time] => [fine] => ) 
        [1] => Array ( [day_id] => 2 [plan_id] => 3 [day_num] => 2 [schedule_time] => 1300953120 [checkin_time] => [fine] => ))

相同的结构等。

2 个答案:

答案 0 :(得分:2)

你基本上已经说了一切。您需要在现有arrayloop上创建新的check some condition和{{3}}。

$new_array = array();

foreach($array as $item) {
    if($item['schedule_time'] > something) {
        $new_array[] = $item;
    }
}

我建议阅读文档。

答案 1 :(得分:2)

$mySpecialTimeToMatch = 0; //whatever time you want to match
$newArray = array();
foreach ($arrayScheduledTasks as $task) {
    foreach ($task as $key => $data) {
        if( "schedule_time" == $key && $mySpecialTimeToMatch == $data ) {
            $newArray[] = $task;
        }
    }
}

print_r($newArray);