-1

Need to merge the Multidimensional array into single array, thereby, eleminating the duplicate values taking key as username and values as their user friends details

Array
(
    [Nishanth] => Array
        (

            [0] => Array
                (
                    [ID] => 3
                    [username] => IronMan
                )

            [1] => Array
                (
                    [ID] => 5
                    [username] => SpiderMan
                )

            [2] => Array
                (
                    [ID] => 8
                    [username] => AntMan
                )

            [3] => Array
                (
                    [ID] => 10
                    [username] => BatMan
                )

            [4] => Array
                (
                    [ID] => 11
                    [username] => SuperMan
                )

        )


    [IronMan] => Array
        (
            [0] => Array
                (
                    [ID] => 1
                    [username] => Nishanth
                )

            [2] => Array
                (
                    [ID] => 5
                    [username] => SpiderMan
                )

        )

    [SpiderMan] => Array
        (
            [0] => Array
                (
                    [ID] => 1
                    [username] => Nishanth
                )

            [1] => Array
                (
                    [ID] => 3
                    [username] => IronMan
                )

            [2] => Array
                (
                    [ID] => 8
                    [username] => AntMan
                )

            [3] => Array
                (
                    [ID] => 10
                    [username] => BatMan
                )

            [4] => Array
                (
                    [ID] => 14
                    [username] => Hulk
                )
        )

    [AntMan] => Array
        (
            [0] => Array
                (
                    [ID] => 1
                    [username] => Nishanth
                )

            [1] => Array
                (
                    [ID] => 10
                    [username] => BatMan
                )
        )

    [BatMan] => Array
        (
            [0] => Array
                (
                    [ID] => 1
                    [username] => Nishanth
                )

            [1] => Array
                (
                    [ID] => 5
                    [username] => SpiderMan
                )

            [2] => Array
                (
                    [ID] => 8
                    [username] => AntMan
                )

            [3] => Array
                (
                    [ID] => 11
                    [username] => SuperMan
                )
        )

    [SuperMan] => Array
        (
            [0] => Array
                (
                    [ID] => 1
                    [username] => Nishanth
                )

            [1] => Array
                (
                    [ID] => 10
                    [username] => BatMan
                )
        )

    [Hulk] => Array
        (
            [0] => Array
                (
                    [ID] => 5
                    [username] => SpiderMan
                )

        )
)

How should I need to merge the above array as shown below

Expected Result:

[MergedUser] => Array
    (
        [0] => Array
            (
                [ID] => 3
                [username] => IronMan
            )

        [1] => Array
            (
                [ID] => 5
                [username] => SpiderMan
            )

        [2] => Array
            (
                [ID] => 8
                [username] => AntMan
            )

        [3] => Array
            (
                [ID] => 10
                [username] => BatMan
            )

        [4] => Array
            (
                [ID] => 11
                [username] => SuperMan
            )

        [5] => Array
            (
                [ID] => 14
                [username] => Hulk
            )
    )

Making use of array_unique() would print as it is. How should we need to achieve this?

Nɪsʜᴀɴᴛʜ ॐ
  • 2,756
  • 4
  • 33
  • 57
  • You forgot `Array ( [ID] => 1 [username] => Nishanth )` in your output array. Am i right? As well as i am unable to understand the logic of achieving your goal. Can you please explain it a bit more? – Alive to die - Anant May 21 '19 at 08:02
  • That should not be supposed to print I had changed in the SQL query. It doesn't matter whether if its included or not as from the expected result in the array. Since I'm retrieving user's friends list that's why. Only thing is I need is to get rid of duplicate values in an array @AlivetoDie – Nɪsʜᴀɴᴛʜ ॐ May 21 '19 at 08:06
  • After the input array is flattened (`array_merge(...$array)`), then [determine uniqueness by a column value](https://stackoverflow.com/q/45603614/2943403). – mickmackusa May 19 '22 at 06:46

4 Answers4

1
<?php
$people =
[
    [
        [
            'id'=>3,
            'name'=>'George'
        ],
        [
            'id'=>5,
            'name'=>'Ringo'
        ]
    ],
    [
        [
            'id'=>3,
            'name'=>'George'
        ],
        [
            'id'=>7,
            'name'=>'Paul'
        ]
    ],
    [
        [
            'id'=> 9,
            'name'=> 'John'
        ]
    ]
];

$peeps = array_merge(...$people);
$peeps = array_column($peeps, null, 'id');
var_export($peeps);

Output:

array (
    3 => 
    array (
      'id' => 3,
      'name' => 'George',
    ),
    5 => 
    array (
      'id' => 5,
      'name' => 'Ringo',
    ),
    7 => 
    array (
      'id' => 7,
      'name' => 'Paul',
    ),
    9 => 
    array (
      'id' => 9,
      'name' => 'John',
    ),
  )

array_column, will filter out duplicates by using the id as the keys for the final array.

Thanks to Alive to Die for noting this will break with your original format. As the array unpacking fails with string keys, this can be fixed by a call of array_values first, so in your case:

$output = array_merge(...array_values($array));
$output = array_column($output, null, 'ID');
Progrock
  • 7,373
  • 1
  • 19
  • 25
  • You have changed OP input array format? Why?Anything wrong there? – Alive to die - Anant May 21 '19 at 08:14
  • 2
    With OP given input array your code will throw fatal error:- https://3v4l.org/vQp6h – Alive to die - Anant May 21 '19 at 08:15
  • Thanks for noticing. Was aiming for brevity here, the OP's input breaks when unpacking as it has string keys (that I omitted), added correction/example to my solution, calling array_values before unpacking fixes this. If sequential keys are really needed for the final array, another call to array_values is required. – Progrock May 21 '19 at 11:39
1

You can use array_map and foreach' to remove duplicated from the existingarray`

$res['MergedUser'] = [];
array_map(function($v) use (&$res){
 foreach($v as $value){
    if(!array_key_exists($value['ID'], $res['MergedUser']))
        $res['MergedUser'][$value['ID']] = $value;
 }
}, $arr);

Live Demo

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
1

You can use function to achieve the same, please see inline doc.

function unique_multidim_array($array, $key)
{
    $temp_array = [];
    $i          = 0;
    $key_array  = [];
    foreach ($array as $val) {
        foreach ($val as $key1 => $value1) {
            if (!in_array($value1[$key], $key_array)) { // will check if already in array
                $key_array[$i]  = $value1[$key]; // once added then wont be added again
                $temp_array[$i] = $value1; // result array
            }
            $i++;
        }
    }
    $ret['MergedUser'] = $temp_array;
    return $ret;
}
$temp = unique_multidim_array($array, "ID"); // unique by which key

Working demo.

Rahul
  • 18,271
  • 7
  • 41
  • 60
0

You can do following to generate unique array.

array_unique($YOUR_ARRAY_VARIABLE, SORT_REGULAR);

this way only unique value is there in your array instead of duplication.

<?php
 // define array 
 $a = array(1, 5, 2, 5, 1, 3, 2, 4, 5); 

 // print original array 
 echo "Original Array : \n"; 
 print_r($a); 

 // remove duplicate values by using  
 // flipping keys and values 
 $a = array_flip($a); 

 // restore the array elements by again  
 // flipping keys and values. 
 $a = array_flip($a); 

 // re-order the array keys 
 $a= array_values($a); 

 // print updated array 
 echo "\nUpdated Array : \n "; 
 print_r($a); 
?>

I hope this link will helps you.

Niket Joshi
  • 739
  • 5
  • 23