23

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]);

6 Answers 6

39

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]
    }

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

2 Comments

Thanks! I copy pasted the first line of code. It works like expected.
array_keys Will be more help up
12

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.

1 Comment

an array is like this (0 =>(student_id=>10215, roll_no => 23), 1 =>(......)) How do I search the student_id of the key which has a roll no. 23.
8

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

Comments

5

You can achieve that using array_search() by using while loop and the following workaround:

while (($key = array_search("2009-09-09", $list[0])) !== FALSE) {
  print($key);
  unset($list[0][$key]);
}

Source: cue at openxbox at php.net

For one-multidimensional array, you may use the following function to achieve that (as alternative to array_keys()):

function array_isearch($str, $array){
  $found = array();
  foreach ($array as $k => $v) {
    if (strtolower($v) == strtolower($str)) {
      $found[] = $k;
    }
  }
  return $found;
}

Source: robertark, php.net

Comments

2

The PHP manual states in the Return Value section of the array_search() function documentation that you can use array_keys() to accomplish this. You just need to provide the second parameter:

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

Comments

0
$userdb=Array
(
    (0) => Array
        (
            (uid) => '100',
            (name) => 'Sandra Shush',
            (url) => 'urlof100'
        ),
);

$key = array_search(100, array_column($userdb, 'uid'));

1 Comment

Please add some explanation.

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.