0

There is a similar question which checks to see whether a given string value exists multiple times within an array.

However, I have an array of arrays. Example:

$testing = [
    1 => ['a' => '', 'b' => '2020-12-04 13:15:36'],
    2 => ['a' => '', 'b' => '2020-12-04 17:15:00'],
    3 => ['a' => '', 'b' => '2021-01-04 17:15:00'],
    2 => ['a' => '', 'b' => '2021-01-04 17:15:00'],
];

The array $testing contains two elements using the key '2', but the value-parts of each of those elements differs.

If I use the approach in the linked example (using array_keys) it reports that each element exists only once in the array; ie. it notices that the value-parts differ and so counts them as unique elements within $testing.

How can I go about testing each array element to check whether the element's key-part exists multiple times in $testing?

That is, I want 1 and 3 to report they are unique within $testing, but 2 to report that it is duplicate - it exists multiple times within $testing (even though the value-part differs in each instance).

Mikhail Prosalov
  • 4,155
  • 4
  • 29
  • 41
youcantryreachingme
  • 1,065
  • 11
  • 17
  • Doens't matter the value is the same or different, you can't have 2 same index in 1 array – catcon Dec 04 '20 at 04:46
  • The reason why array_keys say each element exist once is if you have multiple elements with the same key, the later will override the former. Hence your `$testing` only have 3 items. You can use `print_r($testing)` and see. – catcon Dec 04 '20 at 04:54
  • Thanks. In my application logic I actually have element '2' existing 3 times, and when I iterate through the array, it gets processed twice. However I did just try `print_r` and only the last-defined instance of '2' was printed. Not sure how come the application managed to reference it twice, but regardless, thanks. If you put your explanation in an answer, I will accept it. – youcantryreachingme Dec 04 '20 at 04:59

0 Answers0