-1

I am having 2 arrays and i have to merge that arrays with similar values. i tried for each and basic functions of php for array merging but not getting proper result.

i tried below thing but it wont work for me as i am having multiple data in second array. as you can see in child array i am having multiple records and i want to keep that together inside base array.

    $base= [
        ['id' => 1],
        ['id' => 2],
        ['id' => 3],
        ['id' => 4],
    ];
    
    $child = [
        ['id' => 1, 'size' => 'SM'],
        ['id' => 1, 'size' => 'LK'],
        ['id' => 2, 'size' => 'XL'],
        ['id' => 4, 'size' => 'LG'],
        ['id' => 3, 'size' => 'MD'],
    ];  
    
    foreach(array_merge($base, $child) as $el){
        $merged[$el['id']] = ($merged[$el['id']] ?? []) + $el;
    }
    
    Output :
    array (
      1 => 
      array (
        'id' => 1,
        'size' => 'SM',
      ),
      2 => 
      array (
        'id' => 2,
        'size' => 'XL',
      ),
      3 => 
      array (
        'id' => 3,
        'size' => 'MD',
      ),
      4 => 
      array (
        'id' => 4,
        'size' => 'LG',
      ),
    )

desired output :

array (
  1 => 
  array (
    'id' => 1,
    1 => array('size' => 'SM'),
    2 => array('size' => 'LK'),
  ),
  2 => 
  array (
    'id' => 2,
    1 => array('size' => 'XL'),
  ),
  3 => 
  array (
    'id' => 3,
    1 => array('size' => 'MD'),
  ),
  4 => 
  array (
    'id' => 4,
    1 => array('size' => 'LG'),
  ),
)
amit sutar
  • 541
  • 2
  • 11
  • 37
  • [Have you googled your question before you asked?](https://stackoverflow.com/questions/5881443/merging-arrays-with-the-same-keys) If so where exactly are you stuck at? – Definitely not Rafal Jan 11 '21 at 15:17
  • yes, so much googled but found nothing similar to my problem. and stackoverflow is last hope @D – amit sutar Jan 11 '21 at 15:21
  • Is there any particular use of `$base`? Could there be an id present not in child and an empty array should be added to the result? Also is there a reason you start counting from 1? (The outer most I can imagine are the actual ids but the inner?) – Remy Jan 11 '21 at 15:31
  • its a dummy data . i am not counting from 1 . it would be parent-child relationship from database. base is parent (primarykey) and child is (foreignkey) table. so parent will always have 1 entry and child will be having multiple data or single data. so i have to create json like parent-child.@Remy – amit sutar Jan 11 '21 at 15:40
  • why `$child` have id 4, but `$base` does't have id 4 – Anas Fanani Jan 11 '21 at 15:42
  • @LexaVey edited my post. that was typo. – amit sutar Jan 11 '21 at 15:45

1 Answers1

0

Because your array $child have same key id value, and every you save the array, it always destroyed old array with same key id.

Here the working code :

<?php

$base= [
    ['id' => 1],
    ['id' => 2],
    ['id' => 3],
    ['id' => 4],
];

$child = [
    ['id' => 1, 'size' => 'SM'],
    ['id' => 1, 'size' => 'LK'],
    ['id' => 2, 'size' => 'XL'],
    ['id' => 4, 'size' => 'LG'],
    ['id' => 3, 'size' => 'MD'],
];  
foreach(array_merge($base, $child) as $el){
    if(!isset($el['size'])){ // $base doesn't have 'size' key
        $merged[$el['id']] = []; // fill with empty array 
    }else{
        $merged[$el['id']][] = $el;
    }
    // bellow code is for start array index from 1 instead of 0
    array_unshift($merged[$el['id']],"");
    unset($merged[$el['id']][0]);
}
print_r($merged);
Anas Fanani
  • 685
  • 7
  • 19