PHP - check that a value is not showing twice in an array

I have an array inside my PHP application that looks like this:

Array
(
    [0] => Array
        (
            [name] => Name1
            [language] => 1
        )

    [1] => Array
        (
            [name] => Name2
            [language] => 1
        )

)

      

How can I check that "language" with a value of 1 is not shown twice as much as possible?

+2


a source to share


2 answers


$dupe = 0;
foreach($yourarray as $key => $val) {
    if(array_key_exists($seen, $val['language'])) {
        // a duplicate exists!
        $dupe = 1;
        // could do other stuff here too if you want,
        // like if you want to know the $key with the dupe

        // if all you care about is whether or not any dupes
        // exist, you could use a "break;" here to early-exit
        // for efficiency. To find all dupes, don't use break.
    }
    $seen[$val['language']] = 1;
}

// If $dupe still = 0 here, then no duplicates exist.

      



+3


a source


Tried PHP array_unique function ?



(Read the user comments / notes below, especially the one made by regede in inbox dot ru which made a recursive function for multidimensional arrays)

+1


a source







All Articles