ホームページ >バックエンド開発 >PHPチュートリアル >複数の 2 次元配列を 1 つの配列に統合する方法

複数の 2 次元配列を 1 つの配列に統合する方法

WBOY
WBOYオリジナル
2016-06-23 14:20:591196ブラウズ

二维数组合并

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

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



最後に目標を達成

array(7) {
[0]=> 配列(2) { ["id"]=> 文字列(6) "201307" ["値"]=> int(535) ["値"]=> int(3588) }
[1]=> 配列(2) { ["id"]=> 文字列(6) "201306" ["値"]=> int(5) ["値"]=> int(3177) }
[2]=> 配列(2) { ["id"]=> 文字列(6) "201305" ["値"]=> int(758)["値"]=> int(2946) }
[3]=> 配列(2) { ["id"]=> 文字列(6) "201304" ["値"]=> int(10) ["値"]=> int(2419) }
[4]=> 配列(2) { ["id"]=> string(6) "201303" ["値"]=> int(582) ["値"]=> int(2287) }
[5]=> 配列(2) { ["id"]=> string(6) "201302" ["値"]=> int(390) ["値"]=> int(2251) }
[6]=> 配列(2) { ["id"]=> string(6) "201301" ["値"]=> int(473) ["値"]=> 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] => 配列
(
[id] => 201307
[値] => 535
[値] => 3588


[1] => 配列
(
[id] => 201306
[値] => 5
[値] => 3177
)
[2] => 配列
[id ] => 201305
[値] => 758
[値] => 2946
[3] =>配列
(
[id] => 201304
[value] => 10
[値] => 2419
)
[4] => 配列
(
[id] => 201 303
[値] => 582
[値] => 2287
)
[ 5] => 配列
(
[id] => 201302
[値] => 390
[値] => 22 51
)

[6] =>配列
(
[id] = > 201301
[値] => 473
[値] => 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] => 配列
(
[id] => 201307
[value] => 535
[値] => 3588
)
[1] => 配列
(
[id] => 201306
[値] => 5
[値] => 3177
)
[2] => 配列
(
[id] => 201305
[値] => 758
[値] => 2946
)
[3] => 配列
[id ] => 201304
[値] => 10
[値] => 2419 )

[4] => 配列
(
[id] => 201303
[値] => 582
[値] => 2287
)
[5] => 配列
(
[id] => 201302
[値] => 390 [値] => 2251
[6] => ; 配列
(
[id] => 201301
[値] => ; 473
[値] => 2474
)

)
基本的に重要です。独自の関数はすべてこれらの機能を備えています。牛。私は他の方法を去ります。

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

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

バージョン主 要是条数不对应呢 次の面の记录怎么办! 配列(2) { ["id"]=> 文字列(6) "201307" ["値"]=> int(535) ["値"]=> int(3588) }
[1]=> 配列(2) { ["id"]=> string(6) "201306" ["値"]=> int(3177) }
[2]=> 配列(2) { ["id"]=> 文字列(6) "201305" ["値"]=> int(758)["値"]=> int(2946) }
[3]=> 配列(2) { ["id"]=> 文字列(6) "201304" ["値"]=> int(10) ["値"]=> int(2419) }
[4]=> 配列(2) { ["id"]=> string(6) "201303" ["値"]=> int(582) ["値"]=> int(2287) }
[5]=> 配列(2) { ["id"]=> string(6) "201302" ["値"]=> int(390) ["値"]=> int(2251) }
[6]=> 配列(2) { ["id"]=> string(6) "201301" ["値"]=> int(473) ["値"]=> int(2474) } }



那你就用循環呗

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

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。