F__M
F__M

Reputation: 1598

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 ?

Do I need to loop into the array ?

Thanks.

Upvotes: 0

Views: 38

Answers (1)

Zerium
Zerium

Reputation: 17333

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

$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:

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.

Upvotes: 2

Related Questions