Home > Back-end >  How to merge array using UUID value
How to merge array using UUID value

Time:04-20

I have the following code :

<?php
$arr1 = array();
$arr1[] = ['UUID' => '123a-123a', 'name' => 'A1'];
$arr1[] = ['UUID' => '123b-123b', 'name' => 'B1'];
$arr1[] = ['UUID' => '123c-123c', 'name' => 'C1'];

$arr2 = array();
$arr2[] = ['UUID' => '123a-123a', 'name' => 'A2'];
$arr2[] = ['UUID' => '123b-123b', 'name' => 'B2'];
$arr2[] = ['UUID' => '123c-123c', 'name' => 'C2'];



$new_arr1 = array();
foreach ($arr1 as $key => $value) {
    if(isset($new_arr1[$value['UUID']])){
        $new_arr1[$value['UUID']]  = ['name_a' => $value['name']];
    }else{
        $new_arr1[$value['UUID']] = ['name_a' => $value['name']];
    }
    
}
$new_arr2 = array();
foreach ($arr2 as $key => $value) {
    if(isset($new_arr2[$value['UUID']])){
        $new_arr2[$value['UUID']]  = ['name_1' => $value['name']];
    }else{
        $new_arr2[$value['UUID']] = ['name_2' => $value['name']];
    }
    
}
 $final_array = array_combine($new_arr1, $new_arr2);
var_dump($final_array);

Which give me the following error :

Warning: Array to string conversion in /home/user/scripts/code.php on line 32

Snippet :

https://sandbox.onlinephpfunctions.com/c/cf5fd

I want to use the UUID as an array id.

here is the expected output :

Array
(
    [123a-123a] => Array
        (
            [name_1] => A1
            [name_2] => A2
        )

    [123b-123b] => Array
        (
            [name_1] => B1
            [name_2] => B2
        )

    [123c-123c] => Array
        (
            [name_1] => C1
            [name_2] => C2
        )
)

CodePudding user response:

Instead of use array_combine you need to use array_merge_recursive because is multidimensional

Snippet: https://sandbox.onlinephpfunctions.com/c/eae35

Reference:

CodePudding user response:

No array_combine nor intermediate arrays needed, just construct new array from those you already have:

$arr1 = array();
$arr1[] = ['UUID' => '123a-123a', 'name' => 'A1'];
$arr1[] = ['UUID' => '123b-123b', 'name' => 'B1'];
$arr1[] = ['UUID' => '123c-123c', 'name' => 'C1'];

$arr2 = array();
$arr2[] = ['UUID' => '123a-123a', 'name' => 'A2'];
$arr2[] = ['UUID' => '123b-123b', 'name' => 'B2'];
$arr2[] = ['UUID' => '123c-123c', 'name' => 'C2'];


$final_array = [];
foreach( array_merge($arr1, $arr2) as $entry ){

    if( empty( $final_array[$entry['UUID']] ) )
        $final_array[$entry['UUID']] = ['name_1' => $entry['name']];
    else
        $final_array[$entry['UUID']][ 'name_' . (count( $final_array[$entry['UUID']] )   1) ] = $entry['name'];
}

print_r($final_array);

CodePudding user response:

array_combine creates an array by using one array for keys and another for its values. In your case, each of the 2 arrays have individual subarrays in them. Hence the error when it tries to make one of the subarrays as a string(which it can't).

In your code, you can use a single $new_arr and the make the code more concise with the null coalescing operator(??) like below:

<?php

$new_arr = array();
foreach (array_merge($arr1,$arr2) as $key => $value) {
    $new_arr[$value['UUID']] = $new_arr[$value['UUID']] ?? [];
    $new_arr[$value['UUID']]  = [('name_' . (count($new_arr[$value['UUID']])   1)) => $value['name']];
}

print_r($new_arr);

Online Demo

  •  Tags:  
  • php
  • Related