1

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?

Tim Randall
  • 4,040
  • 1
  • 17
  • 39
therealise
  • 45
  • 3

4 Answers4

2

In addition to other answers, going from

if (in_array("Beautiful People", $thisWeek)  && $key== "Beautiful People")

To

if (in_array("Beautiful People", $thisWeek)  && $value == "Beautiful People")

is redundant since you are looping $thisWeek.

If this statement in_array("Beautiful People", $thisWeek) is false, this one $value == "Beautiful People" will never be true.

Your condition can be simplified to :

if ($value == "Beautiful People")

However, your task is to find if the $value is either "Beautiful People" or "Higher Love"

You can either write it like this :

if ($value == "Beautiful People" || $value == "Higher Love")

Or :

// An array containing the values you are looking for
//                                     |
// Current value +   +-----------------+-----------------+
//               |   |                                   |
//               v   v                                   v
if (in_array($value, [ "Beautiful People", "Higher Love" ]))
Cid
  • 14,968
  • 4
  • 30
  • 45
1

Your code is not working as you want because $key is your current index, and you are comparing it to a value. So you should change:

if (in_array("Beautiful People", $thisWeek)  && $key == "Beautiful People")

to:

if (in_array("Beautiful People", $thisWeek) && $value == "Beautiful People")
Cid
  • 14,968
  • 4
  • 30
  • 45
Lucas Meine
  • 1,524
  • 4
  • 23
  • 32
1

In your test...

if (in_array("Beautiful People", $thisWeek)  && $key== "Beautiful People") {

$key is the index of the line and not the value, so change it to

if (in_array("Beautiful People", $thisWeek)  && $value == "Beautiful People") {

As to why it gives the strange results, have a read of Comparing String to Integer gives strange results

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

You should change, $key with $value:

$thisWeek = array(
    'Dance Monkey',
    'Circles',
    'Beautiful People',
    'Blue Day',
    'Higher Love'
);

foreach ($thisWeek as $key => $value) {
    if (in_array("Beautiful People", $thisWeek)  &&  ($value == "Beautiful People")) {
        echo "$key => $value  - new <br>";
    } else {
        echo "$key => $value <br>";
    }
}

the first condition is equivalent to the second, so you can reduce to:

if (in_array("Beautiful People", $thisWeek))
niac
  • 33
  • 2
  • 17