首页 > 作文

PHP实现数组根据某个字段进行水平合并,横向合并案例分析

更新时间:2023-04-08 06:38:20 阅读: 评论:0

本文实例讲述了php实现数组根据某个字段进行水平合并,横向合并。分享给大家供大家参考,具体如下:

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_mo你认为练书法有什么益处ney] => 0.01      [today_pay_num] => 1      [yesterday_pay_money] => 0.00      [yesterday_pay_num] => 0   东南大学迎新网 )  [2] => array    (      [date] => 2019-05-05      [today_pay_money] =>摘抄100字 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;  el  $res[$v['date']]= array_merge($res[$v['date']],$v);}

本文发布于:2023-04-08 06:38:18,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/04d8bcc56d5c460b02522174ee9c2eeb.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PHP实现数组根据某个字段进行水平合并,横向合并案例分析.doc

本文 PDF 下载地址:PHP实现数组根据某个字段进行水平合并,横向合并案例分析.pdf

下一篇:返回列表
标签:数组   横向   有什么   水平
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图