Home > OS >  Group data from two arrays by their first level key and form indexed subarrays
Group data from two arrays by their first level key and form indexed subarrays

Time:10-20

I have 2 arrays I want to merge together in a specific way. My result(array) is maybe bad format I'm new to programming sorry for that. I'm trying for hours to fix this with like array_merge and other array functions.

The first array have this voor example '2022-10-23' => and the second array have the same '2022-10-23' =>

Then I want them to merge them together on the same date.

Then the result is like this

array(
  '2022-10-28' => 
  array (
    array (
        '2022-10-28' => 
            'datum' => '2022-10-28',
    ),
    array (
        '2022-10-28' => 
            'id_therapeut' => 5,
            'client_id' => 1,
            'notitie_therapeut' => 'ff',
            'datum_therapeut' => '2022-10-28',
    ),    
  ),

But when the second array have not the date inside and first array the date is empty like this only the date inside (it's not realy empty)

  '2022-10-25' => 
  array (
    'datum' => '2022-10-25',
  ),

then I want this as result

  array ( 
    '2022-10-25' => 
    array (
        '2022-10-25' => 
            'datum' => '2022-10-25',
    ),
    array (
        '2022-10-25' => 
            'datum_therapeut' => '2022-10-25',
    ),    
  ),

This is the first array

 array (
  '2022-10-23' => 
  array (
    'id' => 10,
    'user_id' => 1,
    'notitie' => 'Mag niet !',
    'datum' => '2022-10-23',
    'user_role' => 'client',
  ),
  '2022-10-24' => 
  array (
    'id' => 11,
    'user_id' => 1,
    'notitie' => 'Mag al helemaal niet ',
    'datum' => '2022-10-24',
    'user_role' => 'client',
  ),
  '2022-10-25' => 
  array (
    'datum' => '2022-10-25',
  ),
  '2022-10-26' => 
  array (
    'id' => 15,
    'user_id' => 1,
    'notitie' => '26',
    'datum' => '2022-10-26',
    'user_role' => 'client',
  ),
  '2022-10-27' => 
  array (
    'datum' => '2022-10-27',
  ),
  '2022-10-28' => 
  array (
    'datum' => '2022-10-28',
  ),
  '2022-10-29' => 
  array (
    'datum' => '2022-10-29',
  ),
)

This is the second array

array (
  '2022-10-23' => 
  array (
    'id_therapeut' => 4,
    'client_id' => 1,
    'notitie_therapeut' => 'volgende week',
    'datum_therapeut' => '2022-10-23',
  ),
  '2022-10-26' => 
  array (
    'id_therapeut' => 5,
    'client_id' => 1,
    'notitie_therapeut' => 'ff',
    'datum_therapeut' => '2022-10-26',
  ),
  array (
  '2022-10-28' => 
  array (
    'id_therapeut' => 5,
    'client_id' => 1,
    'notitie_therapeut' => 'ff',
    'datum_therapeut' => '2022-10-28',
  ),
)

This is the array(result) I want

    array ( 
        '2022-10-23' => 
      array (
            '2022-10-23' => 
            'id' => 10,
            'user_id' => 1,
            'notitie' => 'Mag niet !',
            'datum' => '2022-10-23',
            'user_role' => 'client',
    )
   
    array (
        '2022-10-23' => 
            'id_therapeut' => 5,
            'client_id' => 1,
            notitie_therapeut' => 'ff',
            'datum_therapeut' => '2022-10-23',
    ),    
  ),

  array ( 
    '2022-10-24' => 
    array (
        '2022-10-24' => 
            'id' => 11,
            'user_id' => 1,
            'notitie' => 'Mag al helemaal niet ',
            'datum' => '2022-10-24',
            'user_role' => 'client',
    ),
    array (
        '2022-10-24' => 
            'datum_therapeut' => '2022-10-24',
    ),    
  ),

  array ( 
    '2022-10-25' => 
    array (
        '2022-10-25' => 
            'datum' => '2022-10-25',
    ),
    array (
        '2022-10-25' => 
            'datum_therapeut' => '2022-10-25',
    ),    
  ),

  '2022-10-26' => 
  array (
    array (
        '2022-10-26' => 
            'id' => 15,
            'user_id' => 1,
            'notitie' => 'ddfd',
            'datum' => '2022-10-26',
            'user_role' => 'client',
    ),
    array (
        '2022-10-26' => 
            'id_therapeut' => 5,
            'client_id' => 1,
            notitie_therapeut' => 'ff',
            'datum_therapeut' => '2022-10-26',
    ),    
  ),

  array ( 
    '2022-10-27' => 
    array (
        '2022-10-27' => 
            'datum' => '2022-10-27',
    ),
    array (
        '2022-10-27' => 
            'datum_therapeut' => '2022-10-27',
    ),    
  ),



  '2022-10-28' => 
  array (
    array (
        '2022-10-28' => 
            'datum' => '2022-10-28',
    ),
    array (
        '2022-10-28' => 
            'id_therapeut' => 5,
            'client_id' => 1,
            'notitie_therapeut' => 'ff',
            'datum_therapeut' => '2022-10-28',
    ),    
  ),

 
    '2022-10-29' => 
    array (
        '2022-10-29' => 
            'datum' => '2022-10-29',
    ),
    array (
        '2022-10-29' => 
            'datum_therapeut' => '2022-10-29',
    ),    
  )
  

CodePudding user response:

It seems you wish to push every item (from each array) into the result array as a newly indexed entry within a subarray keyed by the date.

I don't see how this new, complex structure helps your application. This feels like an XY Problem. Perhaps when you presenting your data, you should just try to access both arrays by the given date instead of trying to merge the data together.

Code: (Demo)

$result = [];
foreach ($first as $date => $row) {
    $result[$date][] = [$date => $row];
}

foreach ($second as $date => $row) {
    $result[$date][] = [$date => $row];
}
var_export($result);
  • Related