Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Map two associative arrays which have different sizes/keys

I want to merge 2 arrays together but I want it in a specific way.
The first array are the dates I want to filter with the second array dates.

$first = [
    '2022-10-23' => '2022-10-23',
    '2022-10-24' => '2022-10-24',
    '2022-10-25' => '2022-10-25',
    '2022-10-26' => '2022-10-26',
    '2022-10-27' => '2022-10-27',
    '2022-10-28' => '2022-10-28',
    '2022-10-29' => '2022-10-29'
];

$second = [
    '2022-10-24' => [
        'id' => 11,
        'user_id' => 1,
        'notitie' => 'Mag al helemaal niet',
        'datum' => '2022-10-24',
        'user_role' => 'client'
    ],
    '2022-10-26' => [
        'id' => 15,
        'user_id' => 1,
        'notitie' => 26,
        'datum' => '2022-10-26',
        'user_role' => 'client'
    ],
];

This is the result I want:

Array
(
    [2022-10-23] => Array
        (
            [1] => 
        )

[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
    (
        [1] => 
    )

[2022-10-26] => Array
    (
        [id] => 15
        [user_id] => 1
        [notitie] => 26
        [datum] => 2022-10-26
        [user_role] => client
    )

[2022-10-27] => Array
    (
        [1] => 
    )

[2022-10-28] => Array
    (
        [1] => 
    )

[2022-10-29] => Array
    (
        [1] => 
    )

)

 // My coding attempt

if ($agendaButtonInfo == "timeGridWeek"){


  $userNotities = array();
  $dagenTussen = array();

  $period = new DatePeriod(
    new DateTime($agendaDatumBegin),
    new DateInterval('P1D'),
    new DateTime($agendaDatumEinde)
);

foreach ($period as $key => $value) {

  
  $dagenTussen[$value->format('Y-m-d')] =  $value->format('Y-m-d');

  
}


  $stmt = $pdo->query("SELECT notitie_client.id, notitie_client.user_id, notitie_client.notitie, notitie_client.datum, user.user_role
  FROM (notitie_client 
  INNER JOIN user ON notitie_client.user_id = user.id) 
  WHERE notitie_client.user_id='$userId' AND notitie_client.datum BETWEEN '$agendaDatumBegin' AND '$agendaDatumEinde' ORDER BY notitie_client.datum ASC;");
  
  if ($stmt->rowCount() > 0) {
    while ($row = $stmt->fetch()) {
 
      /*
      
      echo "<pre>";
        print_r ($row);
        echo "</pre>";
      */ //$userNotitiess[] = array("datumm" => $row["datum"]);
      $userNotities[$row["datum"]] = array( "id" => $row["id"], "user_id" => $row["user_id"], "notitie" => $row["notitie"], "datum" => $row["datum"], "user_role" => $row["user_role"]);

    }
  }


 
  



echo "<pre>";

print_r($dagenTussen);
echo "<pre>";
print_r($userNotities);

/*
print_r  ($dagenTussen);

  
  function myfunction($v1,$v2)
  {
    //print_r ($v2);
   
  
  if ($v1===$v2["datum"])
    {
    return "same";
    }else{
  return "different";
  }
  }

  echo "<pre>";
  print_r(array_map("myfunction",$dagenTussen, $userNotities));
  //print_r(array_merge($dagenTussen,$userNotities));

  /*
  foreach($userNotities as $userNotitie ){
    echo "<pre>";
    print_r ($userNotitie);
    echo "</pre>";
  }
*/
$testt = array("wesley", "darlon");
$people = array("Peter", "Joe", "wesley", "darlon");

//print_r ($userNotities);


foreach ($userNotities as $notatie){
  echo "<pre>";
  

  $notatieDatum[] = $notatie["datum"];

}

print_r ($notatieDatum);



$eindArray = array();



foreach ($dagenTussen as $dag){

  echo "<br>";

  if (in_array($dag, $notatieDatum))
  {
  echo "Match found";
  print_r ($userNotities);
  }
else
  {
  echo "Match not found";
  }
}

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Map the second array values to the first array values.

The null coalescing operator (??) will fallback to a default value when not found in the lookup.

Code: (Demo)

var_export(
    array_map(
        fn($v) => $rows[$v] ?? [],
        $dates
    )
);

Or if for some odd reason you want fallback to those empty 1-keyed elements, use this:

var_export(
    array_map(
        fn($v) => $rows[$v] ?? [1 => ''],
        $dates
    )
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading