0

I am trying to do something like this:

$fetch = array('a', 'b', 'c', 'd', 'e');
$input = array('a', 'c', 'e');

    if (in_array($input, $fetch)) {
        echo "Array Values Exists";
    }

    else {
        echo 'Invalid Values Exists';
    }

Basically, I am trying to see if the values in $input array exists in the $fetch array. But the above results always shows as Invalid Values Exists instead of showing Array Values Exists. When I read the in_array() documentation here, from example #3, I thought I can use array in array for the parameters. Have I understood this wrong? What is the right way to do this?

2
  • 2
    surely array_intersect() or array_diff() are the best functions for this test rather than looping through the values Commented Jan 10, 2014 at 19:17
  • excellent. I wasnt aware of those. array_intersect() will work perfect for me since I am actually using this to match and validate the checkbox values from $_POST and array_intersect() will actually work well to filter out the array values instead of using a foreach statement that will be a few more lines of codes. Thank you for that. Commented Jan 10, 2014 at 19:26

3 Answers 3

2

$needle can be an array, but that means you're looking for the array, not the elements in the array.

$a = array(array('p', 'h'), array('p', 'r'), 'o');

if (in_array(array('p', 'h'), $a)) {
//Note that array('p', 'h') is in the $haystack array, but not 'p' or 'h' alone.
    echo "'ph' was found\n";
}

If you want to check for the elements of the array, you'll need to iterate it somehow (be it with foreach or array_map()).

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

1 Comment

aha.. now I understand. I thought it was checking the values in the array and matching it. Re-reading the documentation after your posts made sense that its actually matching the array itself and not its values. Thanks for explaining this.
1

How about

$fetch = array('a', 'b', 'c', 'd', 'e');
$input = array('a', 'b', 'c');
$flag = true;
foreach($input as $key=>$val){
    if (!in_array($val, $fetch)) {
        $flag = false;
        break;
    }
}

if($flag=== true)
    echo "Values in input exists in fetch";
else
    echo "Values in input does not exists in fetch";

1 Comment

I tested this code and it works perfect! This will be really very useful! My other option was using array_intersect() like Mark Baker suggested above to filter out the values. But your code actually checks each value and returns false even if 1 of them doesnt match. That is excellent. Thank you so much! :)
0

You can't use $input as array.

See below example :

<?php
$a = array(array('p', 'h'), array('p', 'r'), 'o');

if (in_array(array('p', 'h'), $a)) {
 echo "'ph' was found\n";
}

if (in_array(array('f', 'i'), $a)) {
  echo "'fi' was found\n";
}

if (in_array('o', $a)) {
  echo "'o' was found\n";
}

?>

The above example will output:

'ph' was found 'o' was found

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.