亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當值相同時,將 2 個數組合并到第三個新數組中

當值相同時,將 2 個數組合并到第三個新數組中

PHP
白豬掌柜的 2023-06-24 18:30:26
我有一個名為 job counts 的數組,如下$job =>Array        (            [0] => Array([count] => 3[yearmonth] => 2019-7)                    [1] => Array([count] => 3[yearmonth] => 2019-9)                    [2] => Array([count] => 5[yearmonth] => 2019-10))               )第二個日期數組為$date =>Array    ([0] => Array([yearmonth] => 2019-6)         [1] => Array([yearmonth] => 2019-7)         [2] => Array([yearmonth] => 2019-8)         [3] => Array([yearmonth] => 2019-9)         [4] => Array([yearmonth] => 2019-10))作業數組不是連續數組。所以這里我想要的是,如果一個條目在 $date 中可用但在 $job 中不存在,則創建第三個數組,其中 count = 0 和yearmonth 值。像下面這樣的東西Array    ([0] => Array([count] => 0[yearmonth] => 2019-6)         [1] => Array([count] => 3[yearmonth] => 2019-7)         [2] => Array([count] => 0[yearmonth] => 2019-8)         [3] => Array([count] => 3[yearmonth] => 2019-9)         [4] => Array([count] => 5[yearmonth] => 2019-10))
查看完整描述

3 回答

?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

$jobs要在迭代數組時有效地搜索數組中的日期$dates,請創建一個“查找”數組。

查找應該具有代表關系數據的鍵 ( yearmonth)。

使用數組中遇到的每個日期$dates,檢查該yearmonth值是否表示為查找中的鍵 - 如果是,則使用該count值,如果不是,則設置 0。

&變量之前的意思是“通過引用修改——這樣原始數組就會發生變化,而不需要聲明一個新的輸出數組。

??是“空合并運算符”,這允許在變量未聲明時使用后備值null。

代碼:(演示

$jobs = [

    ['count' => 3, 'yearmonth' => '2019-7'],

    ['count' => 3, 'yearmonth' => '2019-9'],

    ['count' => 5, 'yearmonth' => '2019-10'],         

];

$dates = [

    ['yearmonth' => '2019-6'],

    ['yearmonth' => '2019-7'],

    ['yearmonth' => '2019-8'],

    ['yearmonth' => '2019-9'],

    ['yearmonth' => '2019-10'],

];


$lookup = array_column($jobs, 'count', 'yearmonth');


foreach ($dates as &$date) {

    $date['count'] = $lookup[$date['yearmonth']] ?? 0;

}


var_export($dates);

輸出:


array (

  0 => 

  array (

    'yearmonth' => '2019-6',

    'count' => 0,

  ),

  1 => 

  array (

    'yearmonth' => '2019-7',

    'count' => 3,

  ),

  2 => 

  array (

    'yearmonth' => '2019-8',

    'count' => 0,

  ),

  3 => 

  array (

    'yearmonth' => '2019-9',

    'count' => 3,

  ),

  4 => 

  array (

    'yearmonth' => '2019-10',

    'count' => 5,

  ),

)

或者,如果您要求聲明一個新的輸出數組,并且關聯子數組的順序必須與您的問題中的順序相同,那么這是我對相同技術的調整:

代碼:(演示

$lookup = array_column($jobs, null, 'yearmonth');


$result = [];

foreach ($dates as $date) {

    $result[] = $lookup[$date['yearmonth']] ?? ['count' => 0] + $date;

}


var_export($result);

查找數組現在包含完整的子數組數據。 array_column()用作null第二個參數來防止隔離單行,yearmonth用作第三個參數來分配第一級鍵。


foreach 循環不再“通過引用修改”??蘸喜⑦\算符仍然用于避免調用isset(),并且非常適合簡潔地編寫后備數據。count我在包含元素的硬編碼數組和數組之間使用“聯合運算符” $date——避免調用array_merge()或手動編寫['yearmonth' => $date['yearmonth']. 這是一種合適的合并技術,因為鍵對于每個子數組來說都是唯一的且關聯的。


查看完整回答
反對 回復 2023-06-24
?
LEATH

TA貢獻1936條經驗 獲得超7個贊

永遠不要低估能夠在 php 數組中以非常靈活的方式/方式使用鍵的力量。


<?php

//Copied from mickmackusa

$jobs = [

    ['count' => 3, 'yearmonth' => '2019-7'],

    ['count' => 3, 'yearmonth' => '2019-9'],

    ['count' => 5, 'yearmonth' => '2019-10'],         

];

$dates = [

    ['yearmonth' => '2019-6'],

    ['yearmonth' => '2019-7'],

    ['yearmonth' => '2019-8'],

    ['yearmonth' => '2019-9'],

    ['yearmonth' => '2019-10'],

];

//End copy


/*

    Make the keys of $jobs be the same as values

    of yearmonth: (This makes it easy to compare later on)


    Array

    (

        [0] => Array

            (

                [count] => 3

                [yearmonth] => 2019-7

            )


        [1] => Array

            (

                [count] => 3

                [yearmonth] => 2019-9

            )


        [2] => Array

            (

                [count] => 5

                [yearmonth] => 2019-10

            )


        [2019-7] => Array

            (

                [count] => 3

                [yearmonth] => 2019-7

            )


        [2019-9] => Array

            (

                [count] => 3

                [yearmonth] => 2019-9

            )


        [2019-10] => Array

            (

                [count] => 5

                [yearmonth] => 2019-10

            )


    )


*/

foreach($jobs as $key=>$item) {

    $jobs[$item['yearmonth']] = $item;

}


//Create a third array $third_arr based on your $jobs and $dates array

$third_arr = [];

foreach($dates as $item) {

    $key_value = $item['yearmonth'];

    if (isset($jobs[$key_value]['yearmonth'])) {

        //Available in dates and present in $jobs

        //Just copy values from the $jobs item which relates to this yearmonth

        $third_arr[] = $jobs[$key_value];

    }

    else {

        //Available in dates but not present in $job

        $third_arr[] = ['count'=>0, 'yearmonth'=>$key_value];

    }

}

第三個數組的輸出$third_arr:


Array

(

    [0] => Array

        (

            [count] => 0

            [yearmonth] => 2019-6

        )


    [1] => Array

        (

            [count] => 3

            [yearmonth] => 2019-7

        )


    [2] => Array

        (

            [count] => 0

            [yearmonth] => 2019-8

        )


    [3] => Array

        (

            [count] => 3

            [yearmonth] => 2019-9

        )


    [4] => Array

        (

            [count] => 5

            [yearmonth] => 2019-10

        )


)

上面代碼的更壓縮版本如下所示:


foreach($jobs as $key=>$item) {

    $jobs[$item['yearmonth']] = $item;

}

$third_arr = [];

foreach($dates as $item) {

    $kvalue = $item['yearmonth'];

    isset($jobs[$kvalue]['yearmonth']) ? 

    $third_arr[] = $jobs[$kvalue] : $third_arr[] = ['count'=>0, 'yearmonth'=>$kvalue];

}


查看完整回答
反對 回復 2023-06-24
?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

因此,如果您添加count => 0到$date數組和索引,yearmonth您可以索引$job并將yearmonth其合并到$date:


$result = array_merge(array_column(array_map(function($v) { $v['count'] = 0; return $v; },

                                   $date), null, 'yearmonth'),

                      array_column($job, null, 'yearmonth'));


查看完整回答
反對 回復 2023-06-24
  • 3 回答
  • 0 關注
  • 210 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號