您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關使用PHP怎么實現數組根據某個字段進行水平合并,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
將兩個素組中日期相同的合并成一行
數組a
Array ( [0] => Array ( [date] => 2019-04-02 [today_pay_money] => 168.00 [today_pay_num] => 1 [yesterday_pay_money] => 999.00 [yesterday_pay_num] => 1 ) [1] => Array ( [date] => 2019-04-09 [today_pay_money] => 0.01 [today_pay_num] => 1 [yesterday_pay_money] => 0.00 [yesterday_pay_num] => 0 ) [2] => Array ( [date] => 2019-05-05 [today_pay_money] => 0.01 [today_pay_num] => 1 [yesterday_pay_money] => 2.00 [yesterday_pay_num] => 1 ) [3] => Array ( [date] => 2019-05-11 [today_pay_money] => 0.00 [today_pay_num] => 0 [yesterday_pay_money] => [yesterday_pay_num] => 1 ) )
數組B
Array ( [0] => Array ( [date] => 2019-05-07 [today_pay_money1] => 0 [today_pay_num1] => 0 [yesterday_pay_money1] => 0 [yesterday_pay_num1] => 0 ) [1] => Array ( [date] => 2019-05-11 [today_pay_money1] => 0 [today_pay_num1] => 0 [yesterday_pay_money1] => 1 [yesterday_pay_num1] => 1 ) )
需要格式
Array ( [2019-04-02] => Array ( [date] => 2019-04-02 [today_pay_money] => 168.00 [today_pay_num] => 1 [yesterday_pay_money] => 999.00 [yesterday_pay_num] => 1 ) [2019-04-09] => Array ( [date] => 2019-04-09 [today_pay_money] => 0.01 [today_pay_num] => 1 [yesterday_pay_money] => 0.00 [yesterday_pay_num] => 0 ) [2019-05-05] => Array ( [date] => 2019-05-05 [today_pay_money] => 0.01 [today_pay_num] => 1 [yesterday_pay_money] => 2.00 [yesterday_pay_num] => 1 ) [2019-05-11] => Array ( [date] => 2019-05-11 [today_pay_money] => 0.00 [today_pay_num] => 0 [yesterday_pay_money] => [yesterday_pay_num] => 1 [today_pay_money1] => 0 [today_pay_num1] => 0 [yesterday_pay_money1] => 1 [yesterday_pay_num1] => 1 ) [2019-05-07] => Array ( [date] => 2019-05-07 [today_pay_money1] => 0 [today_pay_num1] => 0 [yesterday_pay_money1] => 0 [yesterday_pay_num1] => 0 ) )
代碼實現
先將a,b數組合并,判斷當前日期下是否空,空的話直接賦值,不空的話,將已有素組和當前數組合并
$total = array_merge($a,$b)); $res = array(); foreach ($total as $k => $v) { if (empty($res[$v['date']])) $res[$v['date']] = $v; else $res[$v['date']]= array_merge($res[$v['date']],$v); }
關于使用PHP怎么實現數組根據某個字段進行水平合并就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。