Home > database >  Map two associative arrays which have different sizes/keys
Map two associative arrays which have different sizes/keys

Time:10-19

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";
                    }
                }
            }

CodePudding user response:

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
    )
);

CodePudding user response:

You are using dates as key. Fine, hashmaps are efficient approach but complicated. Even your question is hard to understand. Always try to take simple approach to your solution. Once you get experienced and proficient with technology you are using, so will your ability to use it's tools.

https://www.w3schools.com/php/func_array_merge.asp#:~:text=Definition and Usage,last one overrides the others.

// this is like your example:

https://www.w3schools.com/php/phptryit.asp?filename=tryphp_func_array_merge2

What you are looking for is a function

array_merge($firstArray, $secondArray);

which replaces what ever is in first array with data from second array

  • Related