1

I have a bidimensional array in php like this:

Array (
        [0] => Array
            (
                        [index1] => -1
                        [index2] => 77
                        [description] => 7
                    )
        [1] => Array
            (
                        [index1] => -1
                        [index2] => 77
                        [description] => 5
                    )
        [2] => Array
            (
                        [index1] => -1
                        [index2] => 78
                        [description] => 12
                    )
)

I need to find if there is a duplicate between the first level arrays. But only considering the keys index1 and index2.

In the example above, it should return true because 0 and 1 have the same index1 and index2.

5 Answers 5

2

Try this:

<?php

$a=array(
    array('index1'=>-1,'index2'=>77,'description'=>7),
    array('index1'=>-1,'index2'=>77,'description'=>5),
    array('index1'=>-1,'index2'=>78,'description'=>12)
);


function check($a){
    $data=array();

    foreach($a as $arr){
        if ($data[$arr['index1'].'|'.$arr['index2']]) {
            return true;
        }
        $data[$arr['index1'].'|'.$arr['index2']]=true;
    }
    return false;
}

if (check($a)) {
    echo "duplicates found";
}else{
    echo "no duplicates";
}

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

Comments

1
function hasDupes($array, $delim = '|') {
    $values = array();
    foreach ($array as $v) {
        $v = $v['index1'] . $delim . $v['index2'];
        if (isset($values[$v])) {
            return true;
        }
        $values[$v] = 0;
    }
    return false;
}

No need for nested loops or complete walks (or even multiple walks) over the array, which btw is what most of the suggested array_something functions do internally. Iterate once and stop when you see an element that you've seen before.

2 Comments

You should use a separator, otherwise array(index1=>-1,index2=>77) and array(index1=>-17,index2=>7) would not work for example.
@stewe thx, added. But actually the delimiter must not be the first or last character otherwise array(index1=>'-1|',index2=>77) and array(index1=>'-1',index2=>'|77') would not work. Anyway ...
0

Two methods that can help you:
1. if you just want to remove the duplicates you can use:
http://php.net/manual/en/function.array-unique.php
2. if you want to find the dups, you can scan the array, and "for each" item - check the rest of the array using:
http://php.net/manual/en/function.array-search.php

Comments

0

Not a very efficient algorithm, but the brute force way assuming the outer array is numerically indexed:

function hasDuplicates($array) {
  $count = count($array);
  for ($i = 0; $i < $count; $i++){
    for ($j = $i+1; $j < $count; j++) { // check every later element in the array
      if ($array[i]['index1'] == $array[j]['index1'] && $array[i]['index2'] == $array[j]['index2']) {
        return true;
      }
    }
  }
  return false;
}

Comments

0
  1. Sort the Array using a custom sorting function like http://de3.php.net/manual/de/function.uasort.php
  2. Walk through the sorted array and compare neighbours pairwise, if they are identical return true otherwise false

Comments

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.