How can check multiple values in array in php?

If you're looking to optimize this search with just a comparison check only I would do:

public function items_in_array($needles, $haystack)
{
    foreach ($needles as $needle) {
        if (!in_array($needle, $haystack)) {
            return false;
        }
    }

    return true;
}

if you're willing to do it ugly doing multiple if(in_array(...&&in_array(... is even faster.

I did a test with 100 and another with 100,000 array elements and between 2 and 15 needles using array_intersect, array_diff, and in_array.

in_array was always significantly faster even when I had to do it 15x for different needles. array_diff was also quite a bit faster than array_intersect.

So if you're just trying to search for a couple of things to see if they exist in an array in_array() is best performance-wise. If you need to know the actual differences/matches then it's probably just easier to use array_diff/array_intersect.

Feel free to let me know if I did my calculations wrong in the ugly example below.

true0
"; } $total_time = microtime(true) - $start_time; echo "
"; echo($total_time); $start_time = microtime(true); if ( in_array($n1, $arr) !== false && in_array($n2, $arr) !== false && in_array($n3, $arr) !== false && in_array($n4, $arr) !== false && in_array($n5, $arr) !== false && in_array($n6, $arr) !== false && in_array($n7, $arr) !== false && in_array($n8, $arr) !== false && in_array($n9, $arr) !== false && in_array($n10, $arr) !== false && in_array($n11, $arr) !== false && in_array($n12, $arr) !== false && in_array($n13, $arr) !== false && in_array($n14, $arr) !== false && in_array($n15, $arr)!== false) { echo "
true1
"; } $total_time = microtime(true) - $start_time; echo "
"; echo($total_time); $first_time = $total_time; echo "
"; $start_time = microtime(true); if (empty($diff = array_diff($narr,$arr))) { echo "
true2
"; } $total_time = microtime(true) - $start_time; echo($total_time); print_r($diff); echo "
"; echo "
"; if ($first_time > $total_time) { echo "First time was slower"; } if ($first_time < $total_time) { echo "First time was faster"; } echo "
"; $start_time = microtime(true); if (count(($itrs = array_intersect($narr,$arr))) == count($narr)) { echo "
true3
"; print_r($result); } $total_time = microtime(true) - $start_time; echo "
"; echo($total_time); print_r($itrs); echo "
"; if ($first_time < $total_time) { echo "First time was faster"; } echo "
"; print_r($narr); echo "
"; print_r($arr);

\$\begingroup\$

I want to see if one of two values (a, b) are in an array. Here's my current thought:

$match_array = array('a','b');
$array_under_test = array('b', 'd', 'f');

if (array_intersect($match_array, $array_under_test)) {
  // Success!
}

Any better implementations?

asked Apr 19, 2013 at 21:42

\$\endgroup\$

\$\begingroup\$

If you want to verify that either value is in the $array_under_test, array_intersect may not be the best option. It will continue to test for collisions even after it finds a match.

For two search strings you can just do:

if (in_array('a', $array_under_test) || in_array('b', $array_under_test)) {
  // Success!
}

This will stop searching if 'a' is found in the $array_under_test.

For more than two values, you can use a loop:

foreach ($match_array as $value) {
  if (in_array($value, $array_under_test)) {
    // Success!
    break;
  }
}

answered Apr 19, 2013 at 22:26

How can check multiple values in array in php?

p.s.w.gp.s.w.g

1,9512 gold badges15 silver badges29 bronze badges

\$\endgroup\$

0

\$\begingroup\$

In addition to p.w.s.g's answer (which seems fine):

If you have lots of values you need to find, you can use a hash:

// the values go into keys of the array
$needles = array('value1' => 1, 'value2' => 1, 'value3' => 1);
$haystack = array('test', 'value1', 'etc');

// only go through the array once
$found = false;
foreach($haystack as $data) {
   if (isset($needles[$data])) {
       $found = true; break;
   }
 }

It's worth doing this if you search $haystack a lot of times.

answered Apr 21, 2013 at 6:09

cobrucobru

961 silver badge2 bronze badges

\$\endgroup\$

How do you check for multiple values in an array?

To check if multiple values exist in an array: Use the every() method to iterate over the array of values. On each iteration, use the indexOf method to check if the value is contained in the other array. If all values exist in the array, the every method will return true .

How check array contains in PHP?

The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.

What is In_array function in PHP?

PHP in_array() Function The in_array() function searches an array for a specific value. Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.

What are the 3 types of PHP arrays?

Create an Array in PHP In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index. Associative arrays - Arrays with named keys. Multidimensional arrays - Arrays containing one or more arrays.