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

时间:2022-10-28 16:10:49

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.

1 个解决方案

#1


2  

Assuming 2017-01-02 is an array key, you can do the following:

假设2017-01-02是一个数组键,您可以执行以下操作:

$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 (i.e. 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.

从这种方法中获得的简单性也存在无法存储其他数据的缺点,因此请根据您的需要使用。

#1


2  

Assuming 2017-01-02 is an array key, you can do the following:

假设2017-01-02是一个数组键,您可以执行以下操作:

$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 (i.e. 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.

从这种方法中获得的简单性也存在无法存储其他数据的缺点,因此请根据您的需要使用。