p3ac3
p3ac3

Reputation: 161

Compare and combine 2 arrays in PHP

I have two arrays, first array are the days within the current month and the other is from database result. I want to create a new array where the results from database are grouped by id and fullname then combine the array of days within it.

    //days with month array 
    $current_month = array (
        '2020-07-01',
        '2020-07-02',
        '2020-07-03',
        '2020-07-04',
        '2020-07-05',
        '2020-07-06',
        '2020-07-07',
        '2020-07-08',
        '2020-07-09',
        '2020-07-10',
        '2020-07-11',
        '2020-07-12',
        '2020-07-13',
        '2020-07-14',
        '2020-07-15',
        '2020-07-16',
        '2020-07-17',
        '2020-07-18',
        '2020-07-19',
        '2020-07-20',
        '2020-07-21',
        '2020-07-22',
        '2020-07-23',
        '2020-07-24',
        '2020-07-25',
        '2020-07-26',
        '2020-07-27',
        '2020-07-28',
        '2020-07-29',
        '2020-07-30',
        '2020-07-31'
    );

    //array from database;
    $data = array(
        array(
            'id'       => '76f7a8d9-15f1-4793-b4e6-afb615a95ea8',
            'fullname' => 'Henesy Rameya',
            'date'     => '2020-07-03 14:12:17',
            'no'       => 3
        ),
        array(
            'id'       => '6083ad28-f762-4b8c-9ce2-3cfcd56f2f33',
            'fullname' => 'Karen Bacs',
            'date'     => '2020-07-02 14:27:12',
            'no'       => 2
        ),
        array(
            'id'       => '5e615d94-16ec-414e-8931-927904855f74',
            'fullname' => 'Aaron Jencay',
            'date'     => '2020-07-04 14:27:12',
            'no'       => 5
        ),
        array(
            'id'       => '2b4d6893-dfb2-42c5-83a0-107eb272701d',
            'fullname' => 'Andi Anne',
            'date'     => '2020-07-13 14:27:12',
            'no'       => 5
        ),
        array(
            'id'       => '76f7a8d9-15f1-4793-b4e6-afb615a95ea8',
            'fullname' => 'Henesy Rameya',
            'date'     => '2020-07-13 14:12:17',
            'no'       => 5
        ),
        array(
            'id'       => '6083ad28-f762-4b8c-9ce2-3cfcd56f2f33',
            'fullname' => 'Karen Bacs',
            'date'     => '2020-07-17 14:27:12',
            'no'       => 3
        ),
        array(
            'id'       => '6083ad28-f762-4b8c-9ce2-3cfcd56f2f33',
            'fullname' => 'Karen Bacs',
            'date'     => '2020-07-18 14:27:12',
            'no'       => 5
        )
    );

now, i have created a code that will group the array by id, fullname and combined the days of the month, then i'm trying the add the value of "no" when the date is matched, if not, "no" is 0

    $temp = [];
    for($i=0; $i<count($data); $i++){                   
        $id = $data[$i]['id'];
        $fullname   = $data[$i]['fullname'];
        $r_date     = date('Y-m-d', strtotime($data[$i]['date']));

        for($x=0;$x<count($current_month);$x++){
            $temp[$id][$fullname][$x] = array(
                $current_month[$x],
                ($r_date == $current_month[$x]) ? $data[$i]['no'] : 0 //if match append the no, if not add zero
            );      
            
        }
    }

if i print_r $temp, the array is like this.

    Array
    (
        [76f7a8d9-15f1-4793-b4e6-afb615a95ea8] => Array
            (
                [Henesy Rameya] => Array
                    (
                        //$curret_month array
                        [0] => Array
                            (
                                [0] => 2020-07-01
                                [1] => 0
                            )

                        [1] => Array
                            (
                                [0] => 2020-07-02
                                [1] => 0
                            )

                        [2] => Array
                            (
                                [0] => 2020-07-03 
                                [1] => 0  //this should have a value of 5, because 2020-07-03 from current_month is equal to $r_date(2020-07-03)
                            )
                        ...
                        [11] => Array
                            (
                                [0] => 2020-07-12
                                [1] => 0
                            )

                        [12] => Array
                            (
                                [0] => 2020-07-13
                                [1] => 5 //only this value has been appended which is the last array with id [76f7a8d9-15f1-4793-b4e6-afb615a95ea8] and name [Henesy Rameya]
                            )
                        ...
                        //and so on, same with other group array.

my problem is only one value is appended, the rest becomes zero in every group, like 2020-07-03 should have a pair with value of 3(key 0 of $data), but it becomes zero. only 2020-07-13 has a pair value of 5(key 4 of $data) from the database result array that is group by "id" and "fullname".

sorry for my english, i hope you all get my point

Upvotes: 0

Views: 74

Answers (2)

mail2bapi
mail2bapi

Reputation: 1617

What your code is missing check for existing value. Here is a code that should solve your issue.

<?php

//days with month array 
    $current_month = array (
        '2020-07-01',
        '2020-07-02',
        '2020-07-03',
        '2020-07-04',
        '2020-07-05',
        '2020-07-06',
        '2020-07-07',
        '2020-07-08',
        '2020-07-09',
        '2020-07-10',
        '2020-07-11',
        '2020-07-12',
        '2020-07-13',
        '2020-07-14',
        '2020-07-15',
        '2020-07-16',
        '2020-07-17',
        '2020-07-18',
        '2020-07-19',
        '2020-07-20',
        '2020-07-21',
        '2020-07-22',
        '2020-07-23',
        '2020-07-24',
        '2020-07-25',
        '2020-07-26',
        '2020-07-27',
        '2020-07-28',
        '2020-07-29',
        '2020-07-30',
        '2020-07-31'
    );

    //array from database;
    $dbData = array(
        array(
            'id'       => '76f7a8d9-15f1-4793-b4e6-afb615a95ea8',
            'fullname' => 'Henesy Rameya',
            'date'     => '2020-07-03 14:12:17',
            'no'       => 3
        ),
        array(
            'id'       => '6083ad28-f762-4b8c-9ce2-3cfcd56f2f33',
            'fullname' => 'Karen Bacs',
            'date'     => '2020-07-02 14:27:12',
            'no'       => 2
        ),
        array(
            'id'       => '5e615d94-16ec-414e-8931-927904855f74',
            'fullname' => 'Aaron Jencay',
            'date'     => '2020-07-04 14:27:12',
            'no'       => 5
        ),
        array(
            'id'       => '2b4d6893-dfb2-42c5-83a0-107eb272701d',
            'fullname' => 'Andi Anne',
            'date'     => '2020-07-13 14:27:12',
            'no'       => 5
        ),
        array(
            'id'       => '76f7a8d9-15f1-4793-b4e6-afb615a95ea8',
            'fullname' => 'Henesy Rameya',
            'date'     => '2020-07-13 14:12:17',
            'no'       => 5
        ),
        array(
            'id'       => '6083ad28-f762-4b8c-9ce2-3cfcd56f2f33',
            'fullname' => 'Karen Bacs',
            'date'     => '2020-07-17 14:27:12',
            'no'       => 3
        ),
        array(
            'id'       => '6083ad28-f762-4b8c-9ce2-3cfcd56f2f33',
            'fullname' => 'Karen Bacs',
            'date'     => '2020-07-18 14:27:12',
            'no'       => 5
        )
    );
    
    $temp = [];
    
    // Loop thru all elements of top array
    foreach($dbData as $data){
        $id = $data['id'];
        $fullname = $data['fullname'];
        $rDate = date('Y-m-d', strtotime($data['date']));
        
        // Iterate thr each date from date array
        foreach($current_month as $idx => $aDate){
            $currentNo = 0;

            // Check for "id" existance in temp array
            if(isset($temp[$id][$fullname][$idx][1]) && ($temp[$id][$fullname][$idx][1] > 0)){
                // Present - grabe the value for current date
                $currentNo = $temp[$id][$fullname][$idx][1];
            }

            $temp[$id][$fullname][$idx] = [
                $aDate,
                ($aDate === $rDate)? $data['no'] + $currentNo : 0 + $currentNo ,
            ];
            
        }     
          
    }
    
    print_r($temp);

Upvotes: 1

Cory Collier
Cory Collier

Reputation: 893

try this:


$temp = [];
$days = array_fill_keys($current_month, 0);
foreach ($data as $item) {
    $id         = $item['id'];
    $fullname   = $item['fullname'];
    $no         = $item['no'];
    $r_date     = explode(' ', $item['date'])[0];

    $dates = $days;

    if (! $temp[$id][$fullname]) {
        $temp[$id][$fullname] = $days;
    }

    $temp[$id][$fullname][$r_date] = $no;
}

echo '=========================================';
print_r($temp);

I removed the N^2 issue (nested for loops). I think this example accomplishes what you are looking for (showing the 'no' value for each day relative to the user's id value).

Upvotes: 0

Related Questions