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

时间:2012-07-31 13:55:36

标签: php arrays strtotime timestamp array-map

我有一个双元素数组 - $ 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=)

2 个答案:

答案 0 :(得分:4)

只需使用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
});

答案 1 :(得分:0)

或使用array_map()

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