2014-02-17 37 views
0

我爲事件日曆製作了一個多維數組。問題是如何解決這個數組中的Startdate問題。讀出數組中特定鍵的值

Array 
(
[0] => Array 
    (
     [ID] => 11 
     [Title] => Evenement 1 van 1 dag 
     [Startdate] => 2014-01-01 
    ) 

[1] => Array 
    (
     [ID] => 12 
     [Title] => Evenement 2 van 1 week 
     [Startdate] => 2014-02-01 
    ) 
) 

我可以將Startdate加載到變量嗎?

+1

'$改編[0] [ '開始日期']'?最有可能你想[循環數組](http://www.php.net/manual/en/control-structures.foreach.php)。要了解有關數組的更多信息,請查看[PHP文檔](http://www.php.net/manual/en/language.types.array.php)。 –

+0

你可以,但它只是其中之一。 '$ star_date = $ arr [0] ['Stardate']' –

回答

0

是的,這很容易:

$Startdate = $array[0]['Startdate']; 

哪裏$array是你的陣列的名稱。

更改第一組方括號中的數字以在多維數組中選擇其他StartDates。

0

您可以使用array_map:

$data = Array (...); 

$getDate = function($arr) { 
    return $arr['Startdate']; 
} 

$result = array_map($getDate, $data); 

這給你一個包含所有啓動日期的數組。

http://be2.php.net/manual/en/function.array-map.php

一個更通用的方法:

$property = function($prop) { 
    return function($obj) use ($prop) { 
     return $obj[$prop]; 
    }; 
}; 

$result = array_map($property("StartDate"), $data); 
// Basically, any time you need to map to a property of an object, you can use the $property function and pass the property you want to map to. 
// need to get the titles instead? 
$result = array_map($property("Title"), $data); 
相關問題