I have a problem converting variables into array.
I am running foreach loop to get values from my multidimensional array $images. $images array contains image name eg: “Item Blue.png” or “Item Light Oak.png” and id of each image.
foreach ($images['images'] as $image) {
$image_name = explode(" ", substr_replace($image->filename ,"",-4));
if(!empty($image_name[2])) {
$colour = ucfirst($image_name[1] . " " . $image_name[2]);
}
else {
$colour = ucfirst($image_name[1]);
}
}
$colour variable is giving me Color name and $image->id can give me image id.
I would like to build $colors array with above variables that it would look like this:
$colors = array(
'Blue' => 1620,
'Green' => 1467,
);
Kind of like this:
$colors = array(
'$colour' => $image->id,
);
I have no idea how to do this and I will appreciate any help to give me at least some directions.
Thanks
This should be pretty straightforward … Two things to do:
First initialize the colors array outside of your foreach:
then just add one line after the if/else, still inside your foreach loop that will insert a new item into the $colors array.
This will create a colors array with contents like what you’re looking for. I’m assuming that there is an ‘id’ key in the $image iterator. Did you need to create one?
All that said, you’re not checking for these problems:
Hope that helps