如何在php中连接两个数组
如何将两个数组连接到单个数组?在日期位置0和1都连接在循环中,我的数组代码如下如何在php中连接两个数组,php,arrays,loops,multidimensional-array,Php,Arrays,Loops,Multidimensional Array,如何将两个数组连接到单个数组?在日期位置0和1都连接在循环中,我的数组代码如下 Array ( [Apr-2019] => Array ( [0] => Array ( [DateUser] => Apr-2019 [withdarw_amount] => 4.00 )
Array
(
[Apr-2019] => Array
(
[0] => Array
(
[DateUser] => Apr-2019
[withdarw_amount] => 4.00
)
[1] => Array
(
[current_deposit_amount] => 1.00
[current_deposit_table_refer] => 0.00
[current_deposit_user_refer] => 0.10
[DateUser] => Apr-2019
)
)
)
就像我的输出:
[Apr-2019] => Array
(
[DateUser] => Apr-2019
[withdarw_amount] => 4.00
[current_deposit_amount] => 1.00
[current_deposit_table_refer] => 0.00
[current_deposit_user_refer] => 0.10
[DateUser] => Apr-2019
)
我试过使用这个代码
$data = array_merge($withdrow_amount,$data_casback,$cashbonus_data,$data_discount,$CurrentDeposit);
$months = array();
foreach($data as $date) {
$month = substr($date['DateUser'], 0, 8);
$months[$month][] = $date;
}
echo '<pre>'; print_r($months); die;
$data=array\u merge($withdrow\u amount,$data\u casback,$cashfusbonus\u data,$data\u折扣,$CurrentDeposit);
$months=array();
foreach($数据为$日期){
$month=substr($date['DateUser'],0,8);
$months[$month][]=$date;
}
回声';印刷费(月);死亡
您可以在数组上迭代,使用with展平内部数组。注意:一个数组中不能有两个DateUser
键,因此其中一个将被删除;假设它们的值与您的数据中的值相同,则不会出现问题:
Array
(
[Apr-2019] => Array
(
[DateUser] => Apr-2019
[withdarw_amount] => 4
[current_deposit_amount] => 1
[current_deposit_table_refer] => 0
[current_deposit_user_refer] => 0.1
)
[Jun-2019] => Array
(
[DateUser] => Jun-2019
[withdarw_amount] => 334
)
)
输出:
$new = [];
foreach ($array as $key =>$a) {
$new[$key] = []; // Define with key
foreach ($a as $v) {
$new[$key] += $v; // Concat
}
}
您可以在数组上迭代,使用和来展平内部数组。注意:一个数组中不能有两个
DateUser
键,因此其中一个将被删除;假设它们的值与您的数据中的值相同,则不会出现问题:
Array
(
[Apr-2019] => Array
(
[DateUser] => Apr-2019
[withdarw_amount] => 4
[current_deposit_amount] => 1
[current_deposit_table_refer] => 0
[current_deposit_user_refer] => 0.1
)
[Jun-2019] => Array
(
[DateUser] => Jun-2019
[withdarw_amount] => 334
)
)
输出:
$new = [];
foreach ($array as $key =>$a) {
$new[$key] = []; // Define with key
foreach ($a as $v) {
$new[$key] += $v; // Concat
}
}
您也可以使用简单的循环来实现这一点-
您也可以使用简单的循环来执行此操作-
不清楚。预期的输出是什么?那么到目前为止您尝试了什么?请向我们展示您为达到预期目标所做的努力。@Sougata Bose谢谢,您的回答很好me@Nick谢谢,你的回答很好。我如何对这个数组进行月和年排序?不清楚。预期的输出是什么?那么到目前为止您尝试了什么?请向我们展示您为达到预期目标所做的努力。@Sougata Bose谢谢,您的回答很好me@Nick谢谢,你的回答很好,我如何对这个数组进行月和年排序?如何对这个数组进行月和年排序?@HardikPatel这可能值得一提新问题,但基本上,您可以使用
uksort
函数,将Apr-2019
样式的字符串转换为DateTime
对象(使用date\u从\u格式创建\u),然后对其进行排序。该数组如何按月份和年份进行排序?@HardikPatel这可能值得提出一个新问题,但基本上,您可以使用uksort
函数将Apr-2019
样式字符串转换为DateTime
对象(使用date\u从\u格式创建\u
),然后对其进行排序。该数组如何按月份和年份进行排序?您可以使用usort()
用于排序。该数组如何按月份和年份输出排序?您可以使用usort()
进行排序。