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

时间:2021-11-19 13:41:52

I have a two-element array - $time

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

echo var_dump($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)

How convert each element of $time array to timestamp?

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

echo var_dump($time) should look like:

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 个解决方案

#1


4  

simply use array_walk_recursive

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

#2


0  

Or using array_map()

或者使用array_map()

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

#1


4  

simply use array_walk_recursive

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

#2


0  

Or using array_map()

或者使用array_map()

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