0

My problem is i have a multidimensional array posting from form to php, now i want to checking if duplicate values of some indexes exist in multi dimensional array or not? e.g:

$data=Array
(
    0 => Array
    (
        uid => '100',
        name => 'Sandra Shush',
        type => 'abc'
    ),
    1 => Array
    (
        uid  => '101',
        name => 'Sandra Shushtext',
        type => 'xyz'
    ),
    2 => Array
    (
        uid => '100',
        name => 'Sandra Shush',
        type => 'abc'
    )
);

here name and type of index 1 and 2 are same, so how can i check it?

I am familiar with

$key = array_search('abc', array_column($data, 'type'));

but it is for duplication of single column value in multi rows, in my situation if multi column of same rows same with multi column of any other row then record will be consider as duplicate.

Any help should be appreciated, Thanks in advance.

4 Answers 4

1

You can try using array_reduce by creating a key using your desired item keys:

$result = array_reduce($data, function ($carry, $item) {

    $key = $item['uid'] . $item['type'];

    $item['is_duplicate'] = isset($carry[$key]);

    if ($item['is_duplicate']) {
        $carry[] = $item;
    } else {
        $carry[$key] = $item;
    }

    return $carry;

}, []);

var_dump($result);
Sign up to request clarification or add additional context in comments.

Comments

1

The easiest way, well at least the one I would use is to encode your arrays into md5 (or any other kind) string and compare those values.
I think it is the most efficient in your case.

Example:

<?php

function arrayToString($array) {
    $str = ''; 

    if ( !is_array($array) ) 
        return $str; 

    foreach ( $array as $key => $val ) {
        $str .= $key . ':' . $val; 
    }

    return $str; 
}

$data=Array
(
    0 => Array
    (
        'uid' => '100',
        'name' => 'Sandra Shush',
        'type' => 'abc'
    ),
    1 => Array
    (
        'uid'  => '100',
        'name' => 'Sandra Shush',
        'type' => 'xyz'
    ),
    2 => Array
    (
        'uid' => '100',
        'name' => 'Sandra Shush',
        'type' => 'abc'
    )
);

$temp = array(); 
foreach ( $data as $d ) {
    array_push($temp, md5(arrayToString($d))); 
}

$unique = array_unique($temp); 

var_dump($unique); // prints unique array

Comments

1

This is a very fast designed approach and will find duplicates. Note that duplicates are elements which have the same value for the same key. So if any of uid, name or type match, they will be treated as duplicates. Therefore I adjust the third array element, because all elements in your array share the same values.

$data = [
    ....
    2 =>
        [
            'uid'  => '200',
            'name' => 'Mandra Shush',
            'type' => 'abcx'
        ]
];

$duplicates      = [];
$valuesToCompare = ["uid", "name", "type"];

function equals($value, $toCompare, $keysToCompare)
{
    foreach ($keysToCompare as $compareKey) {
        if ($value[$compareKey] === $toCompare[$compareKey]) {
            return true;
        }
    }

    return false;
}

foreach ($data as $index => $element) {
    foreach ($data as $indexInner => $elementToCompare) {
        if ($index !== $indexInner) {
            if (equals($element, $elementToCompare, $valuesToCompare)) {
                $duplicates[] = [$index => $indexInner];
            }
        }
    }
}

var_dump($duplicates);

This will output the following, which indicates we found 2 duplicates. Where element 0 is duplicate of 1, and 1 is duplicate of 0.

array (size=2)
  0 => 
    array (size=1)
      0 => int 1
  1 => 
    array (size=1)
      1 => int 0

Comments

0

I achieved above scenario like this:

Dont know which one is best mine or other's who posted answers.

foreach($data as $key => $row)
{
    $combinedarr[] = array("name"=>$row["name"],"type"=>$row["type"]);
}
//chck if same facilitiy is being visit on same date twice
$countcomb = count($combinedarr);
$uniquearr = array_unique($combinedarr, SORT_REGULAR);
if($countcomb==count($uniquearr)){
}else{
    //yes duplicate exists
};

Thanks again for those who answered.

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.