PHP如何将数组的每个元素转换为时间戳


PHP How to convert each element of array to timestamp?

我有一个两元素数组-$time

echo var_dump($time):

array
  0 => 
    array
      'otw' => string '12:00' (length=5)
      'zam' => string '15:00' (length=5)
 1 => 
    array
      'otw' => string '16:00' (length=5)
      'zam' => string '18:00' (length=5)

如何将$time数组的每个元素转换为时间戳?

echo var_dump($time)应该看起来像:

array
  0 => 
    array
      'otw' => timestamp 'timestampvalue' (length=)
      'zam' => timestamp 'timestampvalue' (length=)
 1 => 
    array
      'otw' => timestamp 'timestampvalue' (length=)
      'zam' => timestamp 'timestampvalue' (length=)

只需使用array_walk_recursive

array_walk_recursive($your_array, function(&$element) {
  // notice: this will use the date of today and add the time to it.
  $element = strtotime($element);
  // $element = strtotime($element, 0); // use 1.1.1970 as current date
});

或使用array_map()

function arrayToTimestamps($array)
{
    return array(strtotime($array['otw']), strtotime($array['zam']));
}
$newArray = array_map('arrayToTimestamps', $array);