1

First time trying to find something in an array using an array as both a needle and a haystack. So, example of 2 arrays:

My Dynamically formed array:

Array ( 
    [0] => 
    [1] => zpp 
    [2] => enroll 
) 

My Static comparison array:

Array ( 
    [0] => enroll 
) 

And my in_array() if statement:

if (in_array($location_split, $this->_acceptable)) {
   echo 'found';
}
$location_split; // is my dynamic
$this->_acceptable // is my static

But from this found is not printed out, as I'd expect it to be? What exactly am I failing on here?

3 Answers 3

3

If i'm understanding you right, you want to see whether the first array's entries are present in the second array.

You might look at array_intersect, which would return you an array of stuff that's present in all the arrays you pass it.

$common = array_intersect($this->_acceptable, $location_split);
if (count($common)) {
    echo 'found';
}

If the count of that array is at least 1, then there's at least one element in common. If it's equal to your dynamic array's length, and the array's values are distinct, then they're all in there.

And, of course, the array will be able to tell you which values matched.

Sign up to request clarification or add additional context in comments.

2 Comments

I want to see if any of the static array values exist in the dynamic one. But not all or nothing if that makes sense.
@chris: Then check whether the count from the array returned by array_intersect is at least 1. Added a code sample.
1

Because there is no element in your array containing array('enroll') in it (only 'enroll').

Your best bet would be to use array_diff(), if the result is the same as the original array, no match is found.

Comments

0

You are interchanging the parameters for in_array (needle & haystack). It needs to be,

 if(in_array($this->_acceptable, $location_split))
{
  echo 'found';
 }

Edit: Try using array_intersect.

Demo

1 Comment

tried it that way to, thats what I first thought myself, but its the same either direction

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.