0

I want to get the index of an element which I'm looking for using in_array("Tuesday", $day) and get the element from another array on the same position as this one. I have 2 arrays: $day and $action. I need some help with this, I'm stuck.

4
  • It's not really clear what you ask, but array_search might be a good start. Commented Oct 30, 2013 at 21:31
  • Ok, so I'm using the in_array like this: if(in_array("Tuesday, $day)){}. If it is in array, I want to take it's position in the array and print the element on the same position from the other array. Commented Oct 30, 2013 at 21:32
  • the in_array won't return you any index, it's just a boolean, array_search does the same job and returns the key, ex: $key = array_search("Tuesday, $day); if $key > 0 // do stuff (and you can of course do $action[$key] Commented Oct 30, 2013 at 21:34
  • Can you please post this as an answer, so I can accept it? Thank you! Commented Oct 30, 2013 at 21:37

1 Answer 1

1

the in_array won't return you any index, it's just a boolean, array_search does the same job and returns the key, ex:

$key = array_search("Tuesday", $day); 
if($key !== false)
   // do stuff (and you can of course do $action[$key]
Sign up to request clarification or add additional context in comments.

1 Comment

if ($key > 0) is wrong. Just read the documentation: if ($key !== false) is correct.

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.