Php 读取数组中特定键的值
我为事件日历制作了一个多维数组。问题是如何从这个数组中只寻址StartdatePhp 读取数组中特定键的值,php,arrays,Php,Arrays,我为事件日历制作了一个多维数组。问题是如何从这个数组中只寻址Startdate Array ( [0] => Array ( [ID] => 11 [Title] => Evenement 1 van 1 dag [Startdate] => 2014-01-01 ) [1] => Array ( [ID] => 12 [Title] => Even
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加载到变量中吗?是的,非常简单:
$Startdate = $array[0]['Startdate'];
其中,$array
是数组的名称
更改第一组方括号中的数字以选择多维数组中的其他起始日期。您可以使用数组映射:
$data = Array (...);
$getDate = function($arr) {
return $arr['Startdate'];
}
$result = array_map($getDate, $data);
这将为您提供一个包含所有开始日期的数组
一种更通用的方法:
$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);
$arr[0]['Startdate']
?很可能是你想要的。要了解有关数组的更多信息,请查看。您可以,但它只是其中之一<代码>$star\u date=$arr[0]['Stardate']