-2

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";
                    }
                }
            }
1
  • Please always present your sample array data as var_export() text (not var_dump(), not print_r(), and not dd()) so that contributors can use it. Oct 18, 2022 at 20:53

1 Answer 1

-2

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%20and%20Usage,last%20one%20overrides%20the%20others.

// 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

3
  • This preserves the values from the first array (with dates) -- which is not desired in the output. Oct 18, 2022 at 20:52
  • I'm confused. In desired output you are listing elements from first array. Oct 18, 2022 at 20:57
  • All keys from the first array are represented in the output, if a given key is not represented in the second array, then its value needs to default to an empty array (or a single empty element array). Oct 18, 2022 at 20:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.