Dimas Adi Andrea Dimas Adi Andrea - 5 months ago 15
PHP Question

How to use in_array in php with an array as needle but return true when there is at least one value match

Here is my

in_array
code

$array = array('a', 'b', 'c');

if(in_array(array('p', 'c'), $array)){
echo "found";
}else{
echo "not found";
}


it returns not found, but actually I want it to return found, because there is one value match
c
.

Answer

use array_interset():-

$search = array('p', 'c');
$array = array('a', 'b', 'c');

$result = !empty(array_intersect($search , $array ));

var_dump($result); // print result

//OR
if(count($result) >=1){echo 'found';}else{'not found';}

Output:-https://eval.in/599429

Reference:-

http://php.net/manual/en/function.array-intersect.php

Comments