Reputation: 319
I have the following code that I am using on WordPress:
if($terms && !is_wp_error($terms) ) {
$colors = array();
foreach ($terms as $term) {
$colors[] = '\'' . $term->slug . '\'';
}
}
print_r(array_values($thePack));
The variable $color
now is a basic Array, which print_r
displays like this:
Array (
[0] => 'white'
[1] => 'green'
)
I'd like to make a condition in order recognize whether the array has or not a specific value, for example:
if(in_array('white', $colors) {
echo "This is white";
}
However, it is not working at all, because the in_array
does not recognize the value in the array!
How could I make the condition work?
Upvotes: 0
Views: 42
Reputation: 12244
The problem is you are escaping the color into the array. Instead of using
$colors[] = '\''.$term->slug.'\''
Just do
$colors[] = $term->slug
And when you output the slug to the web page or the database, then you escape it.
Upvotes: 0
Reputation: 312
Why not do something like this:
while(list($key, $value) = each($array)){
if($value == 'white'){
echo 'this is white';
}
}
Upvotes: 0
Reputation: 53601
Your array values (the color names) include single quotes, which you need to include when you search for a value:
if(in_array("'white'", $colors) {
// ...
}
Upvotes: 4