I have this array in PHP:
JavaScript
x
$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:
JavaScript
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:
JavaScript
foreach ($items as $item) {
foreach ($item['colors'] as $color) {
if (isset($color['name'])) {
$name = $color['name'];
// do something with it
}
}
}