-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?

3
  • 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? Commented May 21, 2019 at 8: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 Commented May 21, 2019 at 8:06
  • After the input array is flattened (array_merge(...$array)), then determine uniqueness by a column value. Commented May 19, 2022 at 6:46

4 Answers 4

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');
Sign up to request clarification or add additional context in comments.

3 Comments

You have changed OP input array format? Why?Anything wrong there?
With OP given input array your code will throw fatal error:- 3v4l.org/vQp6h
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.
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

Comments

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.

Comments

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.

1 Comment

OP saying about multidimensional array

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.