I am dynamically trying to populate a multidimensional array and having some trouble.
I have a list of US states. Associative array like this $states[nc], $states[sc], etc. in my loop I want to append cities onto each state so $states[nc][cities] contains an array of cities. Im stuck with the logic.
foreach($states as $state) {
$data[$state] = $state;
foreach($cities as $city) {
$data[$state]['cities'] .= $city;
}
}
I know that concatenation is not correct, but I am not sure how to add elements to this array. I keep getting errors with array_push.
What's the correct way to add these elements?
The same way you add to an array when the key is not a concern:
$data[$state]['cities'][] = $city;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With