PHP实现数组根据某个字段进行水平合并,横向合并案例分析
作者:flysnownet 时间:2023-10-04 04:55:53
本文实例讲述了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_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程序设计有所帮助。
来源:https://blog.csdn.net/flysnownet/article/details/90111860
标签:PHP,数组,合并
0
投稿
猜你喜欢
使用JavaScript和CSS实现文本隔行换色的方法
2024-04-19 09:46:52
Python使用PyCrypto实现AES加密功能示例
2022-09-18 13:23:57
Python sklearn中的.fit与.predict的用法说明
2023-01-03 23:54:25
asp自动补全html标签自动闭合(正则表达式)
2013-06-01 20:01:59
vscode使用nuget包管理工具
2023-10-30 13:37:55
Python中使用logging和traceback模块记录日志和跟踪异常
2023-09-20 16:08:51
显示你个性的鼠标指针
2011-06-14 09:41:41
DW CS4初步试用感受
2008-05-30 12:17:00
浅谈Python NLP入门教程
2021-04-18 14:45:53
Python爬取12306车次信息代码详解
2022-05-19 03:42:00
ASP 读取MP3文件帧的信息比特率,采样频率,播放时间
2010-06-04 12:22:00
用JS访问操作iframe框架里的dom
2008-11-10 13:05:00
pytorch网络模型构建场景的问题介绍
2022-07-24 22:38:42
Python通过for循环理解迭代器和生成器实例详解
2022-08-23 14:34:59
asp彩色验证码的制作详解
2007-09-18 13:22:00
Python实现简易的图书管理系统
2021-09-12 06:06:21
Python numpy.array()生成相同元素数组的示例
2023-03-03 17:03:06
Mysql字符集和排序规则详解
2024-01-17 05:27:46
python matlibplot绘制3D图形
2023-05-21 18:47:36
php设计模式之正面模式实例分析【星际争霸游戏案例】
2023-11-14 10:54:04