Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search Array for multiple values in PHP?

I need to get the keys from values that are duplicates. I tried to use array_search and that worked fine, BUT I only got the first value as a hit.

I need to get both keys from the duplicate values, in this case 0 and 2. The search result output as an array would be good.

Is there a PHP function to do this or do I need to write some multiple loops to do it?

$list[0][0] = "2009-09-09";
$list[0][1] = "2009-05-05";
$list[0][2] = "2009-09-09";
$list[1][0] = "first-paid";
$list[1][1] = "1";
$list[1][2] = "last-unpaid";

echo array_search("2009-09-09",$list[0]);
like image 762
Jens Törnell Avatar asked Jul 31 '09 13:07

Jens Törnell


People also ask

How to search multiple values in array PHP?

If you want to find the duplicates as well, you can first make a pass with array_unique, then iterate over that array using array_keys on the original; anything which returns an array of length > 1 is a duplicate, and the result is the keys in which the duplicates are stored.

What is array_keys () used for in PHP?

The array_keys() function returns an array containing the keys.

How do you search an array?

Use filter if you want to find all items in an array that meet a specific condition. Use find if you want to check if that at least one item meets a specific condition. Use includes if you want to check if an array contains a particular value. Use indexOf if you want to find the index of a particular item in an array.

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.


3 Answers

You want array_keys with the search value

array_keys($list[0], "2009-09-09");

which will return an array of the keys with the specified value, in your case [0, 2]. If you want to find the duplicates as well, you can first make a pass with array_unique, then iterate over that array using array_keys on the original; anything which returns an array of length > 1 is a duplicate, and the result is the keys in which the duplicates are stored. Something like...

$uniqueKeys = array_unique($list[0])

foreach ($uniqueKeys as $uniqueKey)
{
  $v = array_keys($list[0], $uniqueKey);

  if (count($v) > 1)
  {
    foreach ($v as $key)
    {
      // Work with $list[0][$key]
    }

  }
}
like image 59
Adam Wright Avatar answered Oct 23 '22 11:10

Adam Wright


In array_search() we can read:

If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use array_keys() with the optional search_value parameter instead.

like image 26
palindrom Avatar answered Oct 23 '22 09:10

palindrom


The following combination of function calls will give you all duplicate values:

$a = array(1, 1, 2, 3, 4, 5, 99, 2, 5, 2);

$unique     = array_unique($a); // preserves keys
$diffkeys   = array_diff_key($a, $unique);
$duplicates = array_unique($diffkeys);

echo 'Duplicates: ' . join(' ', $duplicates) . "\n"; // 1 2 5
like image 8
Till Theis Avatar answered Oct 23 '22 10:10

Till Theis