Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
In PHP, how can I loop only in a part of the multidimensional array ?
For example how to loop into red fruits ?
red
$fruits = array( 'red' => array('apple', 'cherry'), 'yellow' => array('lemon'), 'orange' => array('orange') );
Thanks.
foreach
It's just a normal foreach loop over the inner array. You just need to specify that key rather than iterating the entire $fruits array. Like this:
$fruits
foreach ($fruits['red'] as $red_fruit) { ...
Add a comment
Just loop the item you want with foreach:
foreach($fruits['red'] as $fruit) { // do something }
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
foreach, right? Combine the two.