Looping through a nested array, testing and outputing results with the same structure
Hi I wonder if anyone can point me in the right direction with this problem, I just don't know where to start.
I have an array that follows this structure
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开发者_StackOverflow] => 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] => ) )
What I want to be able to do is loop through it testing 'schedule_time' for specific times, if it matches the whole array is saved into a new array. So say array 0 and 1 matched it would be output like this
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] => ))
Same structure etc.
You basically said everything already. You need to create a new array and loop over the existing one and check some condition.
$new_array = array();
foreach($array as $item) {
if($item['schedule_time'] > something) {
$new_array[] = $item;
}
}
I recommend to read the documentation.
$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);
精彩评论