您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章将为大家详细讲解有关使用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。