简体   繁体   English

从PHP中的多维数组获取键值

[英]Get the key value from a multidimensional array in PHP

I've the following type of array: 我有以下类型的数组:

Array (
  [2017-01-01] => Array (
    [booking_nb] => 0
  )
  [2017-01-02] => Array (
    [booking_nb] => 0
);

How can I get the value of booking_nb if the date is equal to 2017-01-02 ? 如果日期等于2017-01-02如何获取booking_nb的值?

Do I need to loop into the array ? 我是否需要循环到数组中?

Thanks. 谢谢。

Assuming 2017-01-02 is an array key, you can do the following: 假设2017年1月2日是一个数组键,则可以执行以下操作:

$array['2017-01-02']['booking_nb']; // will return the value 0

However, I recommend that if you are only storing the booking_nb value inside of each sub-array (ie no other elements inside the sub-arrays), you simply store them like so: 但是,我建议如果只在每个子数组中存储booking_nb值(即在子数组中没有其他元素),则可以像这样简单地存储它们:

array(
  '2017-01-01' => 0,
  '2017-01-02' => 0,
)

This way, you can select with the following: 这样,您可以选择以下内容:

$array['2017-01-01']; // gives 0

The simplicity gained from this method also has the downside of the inability to store additional data, so use according to your needs. 通过这种方法获得的简便性还具有无法存储其他数据的缺点,因此请根据需要使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM