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?

4 Answers 4

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" ]))
Sign up to request clarification or add additional context in comments.

6 Comments

Yeah, but maybe he should use in_array() before the loop?
Academic exercises can sometimes ask for the weirdest things. I agree it's pointless in this context, but without greater scope of what they are doing it seems like a fudge.
Oh I think I got it.
Pointing out that using in_array makes rather little sense here might be part of the expected result of the exercise, who knows :-)
@04FS yep, I just figured out that part
|
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

Comments

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")

Comments

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.