简体   繁体   中英

How do I add an associative array to an array of an array in PHP?

I have an array called $hours, this array contains 24 other arrays each for the our of the day starting with 00

  • $hours
    • [00]
    • [01]
    • [02]
    • [03]

How would I add a key value pair to the hours arrays so that my end result looks lik:

  • $hours
    • [00]
      • key => value
      • key => value
    • [01]
    • [02]
    • [03]

You should try to use native PHP date datetime class

But if you really need to learn this,

To add a value in $hours at the index "00"

$hours['00']['key'] => $value

Where 'Key' is the key of your second node level array. By what you said I think you need a 3rd node level array.

So

$hours['00']['key'] => array('value 1','value 2');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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