How to show duplicate data in multidimensional array using PHP

php remove duplicates from multidimensional array by key value
php find duplicate values in associative array
php count duplicates in multidimensional array
php get duplicate values in multidimensional array
php array unique multidimensional
php search multidimensional array for multiple values
find duplicate values in array php
php merge multidimensional array by key => $value

I am using Spout Excel reader to read Excel files from php code and saving into a multidimensional array in PHP variable,Array looks like this

$array = [
[
    'id[0]' => 'BX-78',
    'Name[0]' => 'XXX',
    'Address[0]' => 'YUUSATD'
],
[
    'id[1]' => 'BX-79',
    'Name[1]' => 'YYY',
    'Address[1]' => 'DHJSHDJGY'
],
[
    'id[2]' => 'BX-80',
    'Name[2]' => 'ZZZ',
    'Address[2]' => 'DDSDSDA'
]
[
    'id[3]' => 'BX-78',
    'Name[3]' => 'AAA',
    'Address[3]' => 'FSDSDS'
][
    'id[4]' => 'BX-81',
    'Name[4]' => 'XXX',
    'Address[4]' => 'DSDSDSD'
]];

Now i want to show duplicate data from above array using two keys ['id'] and ['name'] if id repeats show as duplicate data,

  • If name repeats show that row as duplicate data if both are duplicate show as again duplicate row
  • Otherwise it is unique row.

I have tried using multidimensional array sorting but it is using only one key to match data in rows.

foreach ($arrExcelData as $v) {
   if (isset($arrExcelData[$v[0]])) {
       // found duplicate
       continue;
    }
    // remember unique item
    $arrExcelData3[$v[0]] = $v;
}

// if you need a zero-based array, otheriwse work with $_data
$arrExcelData2 = array_values($arrExcelData3);

Edited : Expected Output Result :

Matching Rows:

Id       Name    Address
-------------------------
BX-78    XXX     YUUSATD
BX-78    AAA     DDSDSDA
BX-81    XXX     DSDSDSD`

If you want to list the duplicate values, I think the address of the second match should be FSDSDS as there is not item with name AAA and value DDSDSDA:

BX-78    AAA     FSDSDS

If that is the case, what you could do is to first use a double foreach to mark the arrays that contain a duplicate id or name by for example adding a property named id and name except when the array is itself in the second loop.

After this loop, you can tell which arrays are the duplicate ones. Instead of using a corresponding index 0 as in id[0], I have used reset and next so it is not tied to these indexes.

To get the filtered result you could use array_reduce to check for the array keys and unset them.

For example:

foreach ($array as $index => $a) {
    foreach ($array as $v) {
        if ($v === $a) continue;
        if (reset($v) === reset($a)) $array[$index]["id"] = "duplicate";
        if (next($v) === next($a)) $array[$index]["name"] = "duplicate";
    }
}

$array = array_reduce($array, function($carry, $item) {
    if (array_key_exists("id", $item) || array_key_exists("name", $item)) {
        unset($item["id"], $item["name"]);
        $carry[] = $item;
    }
    return $carry;
}, []);

print_r($array);

Result

Array
(
    [0] => Array
        (
            [id[0]] => BX-78
            [Name[0]] => XXX
            [Address[0]] => YUUSATD
        )

    [1] => Array
        (
            [id[3]] => BX-78
            [Name[3]] => AAA
            [Address[3]] => FSDSDS
        )

    [2] => Array
        (
            [id[4]] => BX-81
            [Name[4]] => XXX
            [Address[4]] => DSDSDSD
        )

)

See a php demo

array_unique for multidimensional array – James' Desk, But still it can be used in a multi-dimensional array with a small Scenario 1: Remove duplicates from array comparing all keys and values in� To merge the duplicate value in a multidimensional array in PHP, first, create an empty array that will contain the final result. Then we iterate through each element in the array and check for its duplicity by comparing it with other elements. If duplicity is found then first merge the duplicate elements and then push it to the final array else directly push to the final array.

I've this very pragmatic approach:

$spout_output = [
[
    'id[0]' => 'BX-78',
    'Name[0]' => 'XXX',
    'Address[0]' => 'YUUSATD'
],
[
    'id[1]' => 'BX-79',
    'Name[1]' => 'YYY',
    'Address[1]' => 'DHJSHDJGY'
],
[
    'id[2]' => 'BX-80',
    'Name[2]' => 'ZZZ',
    'Address[2]' => 'DDSDSDA'
],
[
    'id[3]' => 'BX-78',
    'Name[3]' => 'AAA',
    'Address[3]' => 'FSDSDS'
],
[
    'id[4]' => 'BX-81',
    'Name[4]' => 'XXX',
    'Address[4]' => 'DSDSDSD'
]];

// store id to row, and name to row mappings.
// id and name will be keys, value will be an array of indexes of the array $spout_output
$id_to_rows = array();
$name_to_rows = array();

$duplicate_ids = array();
$duplicate_names = array();

foreach($spout_output as $row => $data)
{
    $key_id = 'id['.$row.']';
    $key_name = 'Name['.$row.']';

    if(!isset($data[$key_id]))
        continue;

    $value_id = $data[$key_id];
    $value_name = $data[$key_name];

    if(!isset($id_to_rows[$value_id]))
    {
        $id_to_rows[$value_id] = array();
    }
    else
    {
        if(!isset($duplicate_ids[$value_id]))
        {
            $duplicate_ids[$value_id] = $id_to_rows[$value_id];
        }

        $duplicate_ids[$value_id][] = $row;
    }

    if(!isset($name_to_rows[$value_name]))
    {
        $name_to_rows[$value_name] = array();
    }
    else
    {
        if(!isset($duplicate_names[$value_name]))
        {
            $duplicate_names[$value_name] = $name_to_rows[$value_name];
        }

        $duplicate_names[$value_name][] = $row;
    }

    $id_to_rows[$value_id][] = $row;
    $name_to_rows[$value_name][] = $row;
}

echo 'Duplicates:';
echo '<br>';
$shown_rows = array();
foreach($duplicate_ids as $id => $rows)
{
    foreach($rows as $nr)
    {
        echo $id . '|' . $spout_output[$nr]['Name['.$nr.']'] . '|' . $spout_output[$nr]['Address['.$nr.']'];
        echo '<br>';
        $shown_rows[] = $nr;
    }
}

foreach($duplicate_names as $name => $rows)
{
    foreach($rows as $nr)
    {
        // if already shown above, skip this row
        if(in_array($nr, $shown_rows))
            continue;

        echo $spout_output[$nr]['id['.$nr.']'] . '|' . $spout_output[$nr]['Name['.$nr.']'] . '|' . $spout_output[$nr]['Address['.$nr.']'];
        echo '<br>';
        $shown_rows[] = $nr;
    }
}

Outputs:

Duplicates:
BX-78|XXX|YUUSATD
BX-78|AAA|FSDSDS
BX-81|XXX|DSDSDSD

I think your 'wanted output' contains an error in the address? Anyway, with my code above I think you'll have enough mapped data to produce the output you want.

How to merge the duplicate value in multidimensional array in PHP , To merge the duplicate value in a multidimensional array in PHP, first, create an empty array that will contain the final result. Then we iterate through each element in the array and check for its duplicity by comparing it with other elements. Remove Duplicate values from multidimensional array using function; PHP remove duplicates from the multidimensional array by key-value; Remove Duplicate values from multidimensional array using function. Here we will take an example to remove duplicate elements or values from a multidimensional array using PHP functions.

You could do something like this:

$dupes = [];
$current = [];

foreach ($array as $index => $entry) {
  $idKey = "id[$index]";
  $nameKey = "Name[$index]";
  if (array_key_exists($entry[$idKey], $current)) {
    $dupes[] = [$entry, $current[$entry[$idKey]]];
  }
  elseif (array_key_exists($entry[$nameKey], $current)) {
    $dupes[] = [$entry, $current[$entry[$nameKey]]];
  }
  else {
    $current[$entry[$idKey]] = $current[$entry[$nameKey]] = $entry;
  }
}

print_r($dupes);

Which results in an array containing each set of duplicates (array of arrays):

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id[3]] => BX-78
                    [Name[3]] => AAA
                    [Address[3]] => FSDSDS
                )

            [1] => Array
                (
                    [id[0]] => BX-78
                    [Name[0]] => XXX
                    [Address[0]] => YUUSATD
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id[4]] => BX-81
                    [Name[4]] => XXX
                    [Address[4]] => DSDSDSD
                )

            [1] => Array
                (
                    [id[0]] => BX-78
                    [Name[0]] => XXX
                    [Address[0]] => YUUSATD
                )

        )

)

Demo here: https://3v4l.org/JAtNU

php check multidimensional array for duplicate values � GitHub, echo "find completely identical arrays\n";. foreach ($arrays as $current_key => $ current_array) { echo "\n\nfind arrays with duplicate value for 'name'\n";. I am working with a one dimensional array in PHP. I would like to detect the presence of duplicate values, then count the number of duplicate values and out put the results. For example, given the

array_unique - Manual, array_unique — Removes duplicate values from an array Create multidimensional array unique for any single key index. I find it odd that there is no version of this function which allows you to use a comparator callable in order to� The result will be a one dimensional array no matter how many levels the multidimensional array has. Step 2: Make the values unique. To make this one dimensional array unique, use array_unique() function. Step 3: Revert it to the multidimensional array. Though the array is now unique, the values looks like byte stream representation.

Removing duplicate values in Multi-Dimensional Array? - PHP, How to remove duplicate values from a multi-dimensional array in PHP Can you help me tweak that codes so it will find duplicate values in lessonid only? If that harms you ability to use it, then you simply need to just iterate over the $ result� Version Description; 7.2.0: If sort_flags is SORT_STRING, formerly array has been copied and non-unique elements have been removed (without packing the array afterwards), but now a new array is built by adding the unique elements.

Iterate over multi-dimensional array to find duplicates?, I have a multi-dimensional array that i want to loop through and find I know that i can initialize an array variable $duplicates to store IDs of duplicates and then use an $reduced = $data->groupBy('sku')->map(function ($items) { $item function - http://php.net/manual/en/function.array-walk-recursive.php. PHP - Multidimensional Arrays. A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.

PHP remove duplicates from multidimensional array, When we work with PHP or PHP frameworks like laravel, Codeigniter, Remove Duplicate values from multidimensional array using function� Arrays or sub-arrays in multidimensional arrays can be accessed using multiple dimensions. Dimensions: Dimensions of multidimensional array indicates the number of indices needed to select an element. For a two dimensional array two indices to select an element. Two dimensional array: It is the simplest form of a multidimensional array. It can

Comments
  • So, you want to which where either the name or the ID exists in another element of the array?
  • Hello Qirel Thanks for reply,
  • See i want to find duplicate enteries using two key/parameters ['id'] and ['name'] if i find duplicate i want to show that data
  • My point is, is it duplicate if both the ID and name is the same, i.e. you have two rows with the ID BX-78 and the name XXX? Or, if either is found multiple times?
  • Yeah, basically, please provide the expected result from that input you shared.
  • Very nice approach!