欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP實現(xiàn)數(shù)組根據(jù)某個字段進行水平合并,橫向合并案例分析

 更新時間:2019年10月08日 11:23:01   作者:flysnownet  
這篇文章主要介紹了PHP實現(xiàn)數(shù)組根據(jù)某個字段進行水平合并,橫向合并,結(jié)合具體案例形式分析了php數(shù)組遍歷、合并等相關操作技巧,需要的朋友可以參考下

本文實例講述了PHP實現(xiàn)數(shù)組根據(jù)某個字段進行水平合并,橫向合并。分享給大家供大家參考,具體如下:

PHP數(shù)組水平合并,橫向合并,兩條數(shù)據(jù)合并成一行

需求

將兩個素組中日期相同的合并成一行

數(shù)組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
    )
)

數(shù)組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
    )
)

代碼實現(xiàn)

先將a,b數(shù)組合并,判斷當前日期下是否空,空的話直接賦值,不空的話,將已有素組和當前數(shù)組合并

$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相關內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)組(Array)操作技巧大全》、《php排序算法總結(jié)》、《PHP數(shù)據(jù)結(jié)構與算法教程》、《php程序設計算法總結(jié)》、《php字符串(string)用法總結(jié)》及《PHP常用遍歷算法與技巧總結(jié)

希望本文所述對大家PHP程序設計有所幫助。

相關文章

最新評論