I am trying to add a string to a value in my foreach loop for 2 specific values. I have to use the function in_array()
because it is a school exercise. For some reason, it's not working how I want it to work.
I have tried to use an if
statement when the key matches the value, but it's adding the string to the first value of the index, instead of the specific value I want from the array.
Description: Print the array and add the string "new" to Beautiful People and Higher Love. Use the function in_array()
. Other functions aren't allowed.
$thisWeek = array(
'Dance Monkey',
'Circles',
'Beautiful People',
'Blue Day',
'Higher Love'
);
foreach ($thisWeek as $key => $value) {
if (in_array("Beautiful People", $thisWeek) && $key== "Beautiful People") {
echo "$key => $value - new <br>";
}
else {
echo "$key => $value <br>";
}
}
Expected output should be:
0 => Dance Monkey
1 => Circles
2 => Beautiful People - new
3 => Blue Day
4 => Higher Love
I'm getting this:
0 => Dance Monkey - new
1 => Circles
2 => Beautiful People
3 => Blue Day
4 => Higher Love
Also, I have to put the string "new" besides Higher Love. I am testing one value now. Why isn't it working and the code is adding the string to the first key-value instead of the correct one?