循环遍历数组并比较时间戳

I have two arrays that I like to loop through and compare the dates between each array.

$newArray = Array (
    [0] => Array (
            [id] => 1
            [date] => 26-11-2013 9:30:56 PM
        )
    [1] => Array (
            [id] => 2
            [date] => 30-11-2013 11:20:12 AM
        )
    [2] => Array (
            [id] => 3
            [date] => 26-11-2013 9:30:56 PM
        )
    [3] => Array (
            [id] => 4
            [date] => 30-11-2013 11:20:12 AM
        )
}

$oldArray = Array (
    [0] => Array (
            [id] => 1
            [date] => 26-11-2013 9:30:56 PM
        )
    [1] => Array (
            [id] => 2
            [date] => 26-11-2013 9:30:56 PM
        )
    [2] => Array (
            [id] => 3
            [date] => 26-11-2013 9:30:56 PM
        )
}

foreach ($newArray as $newPhoto) {
    foreach ($oldArray as $oldPhoto) {
        if (strtotime($newPhoto['date']) != strtotime($oldPhoto['date'])) {
            // download new photo
        }
    }
}

I realize that placing the foreach with a foreach is not going to cut it. What is the best way to loop through each of these arrays and compare the dates?

The $newArray has the latest photos and compares with the $oldArray if the timestamps do not match or there is a new one in the list, download the new images.

In the example, I would be downloading the second and fourth images and ignoring the others.

I'd do it like this. It does not make any assumptions about the indices of either array, and will be still be fairly efficient as $oldArray grows in size.

// index $oldArray by id for speed
$index = array();
foreach ($oldArray as $photo) {
    $index[$photo['id']] = $photo['date'];
}

// iterate through new photos, checking if each one needs downloading
foreach ($newArray as $photo) {
    if (!isset($index[$photo['id']]) // photo is not in $oldArray
            || strtotime($photo['date']) > strtotime($index[$photo['id']])) { // photo is in $oldArray, but a new version is available
        // download new photo
    }
}
foreach ($newArray as $key => $newPhoto) {
    //If the key doesn't exist in $oldArray it is new
    if(!isset($oldArray[$key])){
      //download new photo
    }
    elseif (strtotime($newPhoto['date']) != strtotime($oldArray[$key]['date'])) { //timestamp mismatch
      //download new photo
    }
}

Note: I assume that your oldArray and newArray are keyed the same. I.e if id 1 is in the 0 spot of newArray I assume that id 1 is in the 0 spot in oldArray.

Why use foreach, you can simply use for:

for($i=0; $i<sizeof($newArray); $i++){
    if(strtotime($newArray[$i]['date']) != strtotime($oldArray[$i]['date'])){
        //new photo do sth
    }else {
        //old photo do sth or not :)
    }
}