Jump to content

[SOLVED] adding values to an array in a foreach loop.


pocobueno1388

Recommended Posts

Hello =]

 

I am creating a function for my horse site that will choose the color of the horse. I have a database full of horse breeds and all the possible colors they can be.

 

Here is what the "color" column might hold for one of the horse breeds:

solid,paint

 

The word "solid" can be a bunch of different colors...so I am trying to make my script add all these colors to an array I am populating that holds all the possible colors for that breed.

 

Here is my current code for the function with comments to let you know what is going on

<?php

      function color($breed){
       $getColors = mysql_query("SELECT colors FROM i_horses WHERE breed='$breed'");
       $color = mysql_fetch_assoc($getColors);
   
         //put colors into array
         $colors = explode(',',$color['colors']); //<----This now holds an array with the values "solid" and "paint".
         
         foreach ($colors as $key => $val){
       
               //If it finds the word "solid" in the current array, which it WILL, I want it to add colors to the array.
               if ($val == 'solid'){

                  $colors[] = "Bay, Dark Bay,Black,Chestnut,Flaxen Chestnut,Light Chestnut,Liver Chestnut,Red Chestnut,
                               Dun,Bay Dun,Red Dun,Gray,Light Gray,Rose Gray,Steel Gray,Palomino,Sorrel";
               continue;
               }

            echo $key.' - '.$val.'<br>';
         }

      }
?>

 

What happens is it ends up just deleting the word "solid" from the array, and doesn't add the colors.

 

What I would like it to do is replace the word "solid" with all the other colors in the code....I can't seem to get it to do this though.

 

Any help is greatly appreciated xD Thanks!

You will always get the wrong result in that code because you are echoing $val to the page after you have already set it as "solid". Ok, not sure if you want to replace "solid" with that long string or if you want to create array elements for each color in that string. So, I'll give you both solutions:

 

Replace "solid" with one long string:

foreach ($colors as $key => $val){
       
  //If it finds the word "solid" in the current array, which it WILL, I want it to add colors to the array.
  if ($val == 'solid'){

    $colors[$key] = "Bay, Dark Bay,Black,Chestnut,Flaxen Chestnut,Light Chestnut,Liver Chestnut,Red Chestnut,
                               Dun,Bay Dun,Red Dun,Gray,Light Gray,Rose Gray,Steel Gray,Palomino,Sorrel";
  }

  echo "$key - $colors[$key]<br>";
}

 

Replace solid with the colors as array elements:

foreach ($colors as $key => $val){
       
  //If it finds the word "solid" in the current array, which it WILL, I want it to add colors to the array.
  if ($val == 'solid'){

    //Add first color
    $colors[$key] = "Bay";
    $colorList = "Dark Bay,Black,Chestnut,Flaxen Chestnut,Light Chestnut,Liver Chestnut,Red Chestnut,
                  Dun,Bay Dun,Red Dun,Gray,Light Gray,Rose Gray,Steel Gray,Palomino,Sorrel";

    //Merge the other colors on the end of the array
    $colors = array_merge($colors, explode(',', $colorList));

  }
}

foreach ($colors as $key => $val){
  echo "$key - $colors[$key]<br>";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.