Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较并组合PHP中的2个数组_Php_Arrays - Fatal编程技术网

比较并组合PHP中的2个数组

比较并组合PHP中的2个数组,php,arrays,Php,Arrays,我有两个数组,第一个数组是当前月份内的天数,另一个数组来自数据库结果。我想创建一个新数组,其中来自数据库的结果按id和全名分组,然后在其中组合天数数组 //days with month array $current_month = array ( '2020-07-01', '2020-07-02', '2020-07-03', '2020-07-04', '2020-07-05',

我有两个数组,第一个数组是当前月份内的天数,另一个数组来自数据库结果。我想创建一个新数组,其中来自数据库的结果按id和全名分组,然后在其中组合天数数组

    //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
        )
    );
现在,我已经创建了一个代码,它将根据id、全名和月份的天数对数组进行分组,然后在匹配日期时尝试添加“否”值,如果不匹配,“否”为0

我的问题是只附加了一个值,其余值在每个组中都变为零,比如2020-07-03应该有一个值为3的对($data的键0),但它变为零。从按“id”和“fullname”分组的数据库结果数组中,只有2020-07-13的对值为5($data的键4)

对不起,我说的是英语,我希望你们都明白我的意思

试试这个:


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

我删除了N^2问题(嵌套循环)。我认为这个示例完成了您要查找的内容(显示每天相对于用户id值的“否”值)。

代码缺少的内容检查现有值。这里有一个代码可以解决您的问题

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

几乎是答案,组合日期应该是这样[0]=>“2020-07-01”[1]=0,而不是这样[2020-07-01]=0,但仍然感谢您的回复

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