I have this array in PHP:
$items = array( array( "info" => "This is my info", "colors" => array( "type" => "block", array( "name" => "Red", "ref" => "red" ), array( "name" => "Blue", "ref" => "blue" ), ), ), );
I want to loop into the color
array like this:
foreach ($items as $item) { foreach ($item['colors'] as $color) { $color['name']; } }
The problem, is the first result is b
from block
.
How can I change this behavior ?
Thanks.
Advertisement
Answer
You can check whether the name
element exists in the $color
value:
foreach ($items as $item) { foreach ($item['colors'] as $color) { if (isset($color['name'])) { $name = $color['name']; // do something with it } } }