2

Given this array:

Array
(
    [0] => Array
        (
            [id] => 3
            [name] => aaa
        )

    [1] => Array
        (
            [id] => 2
            [name] => bbb
        )

    [2] => Array
        (
            [id] => 3
            [name] => aaa
        )

    [3] => Array
        (
            [id] => 1
            [name] => ccc
        )

    [4] => Array
        (
            [id] => 3
            [name] => aaa
        )        
)

How can I check if a value for a specific key is used more than once and return the key?

For example, I want to know which keys have the repeated "id" sub key. In this case id = 3 appears more than once so the result would be

Array
(
    [0] => 2
    [1] => 4
)

Notice that index 0 is not included because it's not actually repeated since is the first to appear.

5
  • 1
    Create a second array. Loop through the first. For each, check if the id is in the second array. If it is not, add it. If it is, echo the index of the current item (or add that index to a 3rd array to create the array you're showing) Commented May 11, 2018 at 17:02
  • What if id 1 was also repeated, what would the result look like? How would you differentiate between 1 and 3? Commented May 11, 2018 at 17:10
  • @AbraCadaver Then it's repeated index would be added to the final result Commented May 11, 2018 at 17:15
  • So from your example array, if array [1] was id 1 and array [3] was id 1 the result would just be 2,3,4? Commented May 11, 2018 at 17:16
  • @AbraCadaver That's correct Commented May 11, 2018 at 17:23

2 Answers 2

3

Iterate your array and add the id of each item to an array. If you encounter an id that's already in the array, add its key to an array of duplicates.

foreach ($array as $key => $item) {
    if (isset($ids[$item['id']])) {
        $duplicates[] = $key;
    } else {
        $ids[$item['id']] = true;
    }
}

I prefer to use the unique attribute (id in the case) as the key (like $ids[$item['id']]) instead of the value so it can be looked up with isset rather than in_array.

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

3 Comments

i don't why you are using isset($ids[$item['id']]
@whoami to check if the id of the current item has already been added to the array of ids, meaning the current item is a duplicate.
what a brilliant answer, i am daily working on arrays, still panic on array, is nice way to learn array
2

If you are trying to find the repeated values, rather than eliminate them, you could achieve this by combining array_column array_diff_uassoc and array_unique to return an array of the repeated values while maintaining the indexes:

$ids = array_column($originalArray, 'id');
$uniqueIds = array_unique($ids);
$repeatedValuesArray = array_diff_uassoc($ids, $uniqueIds, "key_compare_func");

1 Comment

@MatíasCánepa the edit should work, but an even simpler version would be array_diff_assoc($ids, $uniqueIds); The answer just provides an additional index check.

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.