string(6) "201307" ["value"]=> int(535) } [1]=> array(2) { ["id"]=>">
当前位置:Gxlcms > PHP教程 > 多个二维数组如何整合到一个数组里

多个二维数组如何整合到一个数组里

时间:2021-07-01 10:21:17 帮助过:11人阅读

二维数组合并

array(7) {
[0]=> array(2) { ["id"]=> string(6) "201307" ["value"]=> int(535) }
[1]=> array(2) { ["id"]=> string(6) "201306" ["value"]=> int(5) }
[2]=> array(2) { ["id"]=> string(6) "201305" ["value"]=> int(758) }
[3]=> array(2) { ["id"]=> string(6) "201304" ["value"]=> int(10) }
[4]=> array(2) { ["id"]=> string(6) "201303" ["value"]=> int(582) }
[5]=> array(2) { ["id"]=> string(6) "201302" ["value"]=> int(390) }
[6]=> array(2) { ["id"]=> string(6) "201301" ["value"]=> int(473) } }

array(7) {
[0]=> array(2) { ["id"]=> string(6) "201307" ["values"]=> int(3588) }
[1]=> array(2) { ["id"]=> string(6) "201306" ["values"]=> int(3177) }
[2]=> array(2) { ["id"]=> string(6) "201305" ["values"]=> int(2946) }
[3]=> array(2) { ["id"]=> string(6) "201304" ["values"]=> int(2419) }
[4]=> array(2) { ["id"]=> string(6) "201303" ["values"]=> int(2287) }
[5]=> array(2) { ["id"]=> string(6) "201302" ["values"]=> int(2251) }
[6]=> array(2) { ["id"]=> string(6) "201301" ["values"]=> int(2474) } }



最后想得到

array(7) {
[0]=> array(2) { ["id"]=> string(6) "201307" ["value"]=> int(535) ["values"]=> int(3588) }
[1]=> array(2) { ["id"]=> string(6) "201306" ["value"]=> int(5) ["values"]=> int(3177) }
[2]=> array(2) { ["id"]=> string(6) "201305" ["value"]=> int(758)["values"]=> int(2946) }
[3]=> array(2) { ["id"]=> string(6) "201304" ["value"]=> int(10) ["values"]=> int(2419) }
[4]=> array(2) { ["id"]=> string(6) "201303" ["value"]=> int(582) ["values"]=> int(2287) }
[5]=> array(2) { ["id"]=> string(6) "201302" ["value"]=> int(390) ["values"]=> int(2251) }
[6]=> array(2) { ["id"]=> string(6) "201301" ["value"]=> int(473) ["values"]=> int(2474) } }


回复讨论(解决方案)

$a = array( array("id" => "201307", "value" => 535), array("id" => "201306", "value" => 5), array("id" => "201305", "value" => 758), array("id" => "201304", "value" => 10), array("id" => "201303", "value" => 582), array("id" => "201302", "value" => 390), array("id" => "201301", "value" => 473),);$b = array(  array("id" => "201307", "values" => 3588), array("id" => "201306", "values" => 3177), array("id" => "201305", "values" => 2946), array("id" => "201304", "values" => 2419), array("id" => "201303", "values" => 2287), array("id" => "201302", "values" => 2251), array("id" => "201301", "values" => 2474),);$c = array_map('array_merge', $a, $b);print_r($c);
Array
(
[0] => Array
(
[id] => 201307
[value] => 535
[values] => 3588
)

[1] => Array
(
[id] => 201306
[value] => 5
[values] => 3177
)

[2] => Array
(
[id] => 201305
[value] => 758
[values] => 2946
)

[3] => Array
(
[id] => 201304
[value] => 10
[values] => 2419
)

[4] => Array
(
[id] => 201303
[value] => 582
[values] => 2287
)

[5] => Array
(
[id] => 201302
[value] => 390
[values] => 2251
)

[6] => Array
(
[id] => 201301
[value] => 473
[values] => 2474
)

)

$a = array( array("id" => "201307", "value" => 535), array("id" => "201306", "value" => 5), array("id" => "201305", "value" => 758), array("id" => "201304", "value" => 10), array("id" => "201303", "value" => 582), array("id" => "201302", "value" => 390), array("id" => "201301", "value" => 473),);$b = array(  array("id" => "201307", "values" => 3588), array("id" => "201306", "values" => 3177), array("id" => "201305", "values" => 2946), array("id" => "201304", "values" => 2419), array("id" => "201303", "values" => 2287), array("id" => "201302", "values" => 2251), array("id" => "201301", "values" => 2474),);$c = array_map('array_merge', $a, $b);print_r($c);
Array
(
[0] => Array
(
[id] => 201307
[value] => 535
[values] => 3588
)

[1] => Array
(
[id] => 201306
[value] => 5
[values] => 3177
)

[2] => Array
(
[id] => 201305
[value] => 758
[values] => 2946
)

[3] => Array
(
[id] => 201304
[value] => 10
[values] => 2419
)

[4] => Array
(
[id] => 201303
[value] => 582
[values] => 2287
)

[5] => Array
(
[id] => 201302
[value] => 390
[values] => 2251
)

[6] => Array
(
[id] => 201301
[value] => 473
[values] => 2474
)

)
基础很重要啊。没想到自带的函数都有这些功能。牛。我还想其他的方法去。

array(7) {
[0]=> array(2) { ["id"]=> string(6) "201307" ["value"]=> int(535) }
[1]=> array(2) { ["id"]=> string(6) "201305" ["value"]=> int(758) }
[2]=> array(2) { ["id"]=> string(6) "201304" ["value"]=> int(10) }
[3]=> array(2) { ["id"]=> string(6) "201303" ["value"]=> int(582) }
[4]=> array(2) { ["id"]=> string(6) "201302" ["value"]=> int(390) }
[5]=> array(2) { ["id"]=> string(6) "201301" ["value"]=> int(473) } }

array(7) {
[0]=> array(2) { ["id"]=> string(6) "201307" ["values"]=> int(3588) }
[1]=> array(2) { ["id"]=> string(6) "201306" ["values"]=> int(3177) }
[2]=> array(2) { ["id"]=> string(6) "201305" ["values"]=> int(2946) }
[3]=> array(2) { ["id"]=> string(6) "201304" ["values"]=> int(2419) }
[4]=> array(2) { ["id"]=> string(6) "201303" ["values"]=> int(2287) }
[5]=> array(2) { ["id"]=> string(6) "201302" ["values"]=> int(2251) }
[6]=> array(2) { ["id"]=> string(6) "201301" ["values"]=> int(2474) } }

版主 要是条数不对应呢 想得到下面的记录怎么办!
最后想得到

array(7) {
[0]=> array(2) { ["id"]=> string(6) "201307" ["value"]=> int(535) ["values"]=> int(3588) }
[1]=> array(2) { ["id"]=> string(6) "201306" ["values"]=> int(3177) }
[2]=> array(2) { ["id"]=> string(6) "201305" ["value"]=> int(758)["values"]=> int(2946) }
[3]=> array(2) { ["id"]=> string(6) "201304" ["value"]=> int(10) ["values"]=> int(2419) }
[4]=> array(2) { ["id"]=> string(6) "201303" ["value"]=> int(582) ["values"]=> int(2287) }
[5]=> array(2) { ["id"]=> string(6) "201302" ["value"]=> int(390) ["values"]=> int(2251) }
[6]=> array(2) { ["id"]=> string(6) "201301" ["value"]=> int(473) ["values"]=> int(2474) } }

那你就用循环呗

能帮着写个例子么。版主。我写一上午了。 新手不会啊!

人气教程排行