0

I have a problem converting variables into an array.

I am running a foreach loop to get values from my multidimensional array $images which contains the image name, e.g.: "Item Blue.png" or "Item Light Oak.png" and an 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 so 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.

2
  • can you show us some sample input and output ? Commented Apr 29, 2012 at 5:32
  • print_r($images); Array ( [images] => Array ( [1620] => ProductImage Object ( [filename] => Brancepeth Blue.png [id] => 1620 ) ) Commented Apr 29, 2012 at 5:37

2 Answers 2

1

This should be pretty straightforward ... Two things to do:

First initialize the colors array outside of your foreach:

 $colors=array();   //<-- add this
 foreach ($images['images'] as $image) {
     $image_name = explode(" ", substr_replace($image->filename ,"",-4));
     ...

then just add one line after the if/else, still inside your foreach loop that will insert a new item into the $colors array.

    ...
    else {
        $colour = ucfirst($image_name[1]);
    }
    $colors[$colour]=$image->id;  //<-- add this
}

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:

  1. color names with spaces, like 'light oak'
  2. item names with spaces like 'large item light oak.png'
  3. duplicate colors with different IDs

Hope that helps

Sign up to request clarification or add additional context in comments.

4 Comments

small problem Array ( [0] => Array ( [Blue] => 1620 ) [1] => Array ( [Green] => 1621 ) [2] => Array ( [Red] => 1622 ) [3] => Array ( [White] => 1623 ) [4] => Array ( [Bronze Left] => 1670 ) [5] => Array ( [Bronze Right] => 1671 ) [6] => Array ( [Chrome Left] => 1672 ) [7] => Array ( [Chrome Right] => 1673 ) )
edited the answer. Apologies for the confusion. The line that adds the items to the array was wrong.
filename will be consistent and I might work on that later, here problem is with the code, array have there own key now. The key should be colour.
Hah, and I accidentally edited my reply to your reply, not seeing your new reply. I had a mis-read your desired output .. the answer was updated-- just change the line to read: $colors[$colour]=$image->id;
0

I fear the intention to use colours as keys may be unwise because data collisions will result in lost data in the result array.

Instead use the ids as keys and store the colour as values.

sscanf() is a direct way to isolate the colour string. %*s will match (but not capture the first word in the input string. %[^.] will capture the following non-dot characters.

Code: (Demo)

$result = [];
foreach ($images['images'] as $id => $obj) {
    sscanf($obj->filename, '%*s %[^.]', $colour);
    $result[$id] = ucfirst($colour);
}
var_export($result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.