2012-07-31 27 views
1

我有兩個元素的數組 - $時間PHP如何將數組的每個元素轉換爲時間戳?

回聲的var_dump($時間):

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) 

如何轉換$時間陣列中的每個元素爲timestamp?

回聲的var_dump($時間)應該是這樣的:

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

看一看array_walk_recursive() – 2012-07-31 13:57:25

+0

你不希望我們爲你編碼解決方案,是嗎?也許仔細看看文檔:http://php.net/manual/en/function.array-walk-recursive.php – 2012-07-31 14:02:14

回答

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 
}); 
0

或者使用array_map()

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

這不起作用,因爲'array_walk_recursive'嘗試傳遞第二個參數 – MarcDefiant 2012-07-31 14:07:34

+0

是的,整件事情都是錯的,這就是我的意思。 – 2012-07-31 14:12:47

相關問題