Php 按特定字段合并两个数组
我有一个数组,看起来像 $firstArrayPhp 按特定字段合并两个数组,php,arrays,Php,Arrays,我有一个数组,看起来像 $firstArray [0] => Array( [ID] => 1 [Fruit] => Apple [State] => Ohio ) [1] => Array( [ID] => 2 [Fruit] => Orange [State] => Hawaii ) [2] => Array( [ID] => 3 [Fruit] => Ora
[0] => Array(
[ID] => 1
[Fruit] => Apple
[State] => Ohio
)
[1] => Array(
[ID] => 2
[Fruit] => Orange
[State] => Hawaii
)
[2] => Array(
[ID] => 3
[Fruit] => Orange
[State] => Vermont
)
另一个数组看起来像
$secondArray
[0] => Array(
[ID] => 1
[description] => This is sample description
[Price] => 20
)
[1] => Array(
[ID] => 1
[Fruit] => This is sample description 2
[Price] => 15
)
[2] => Array(
[ID] => 2
[Fruit] => This is the second description
[Price] => 100
)
[3] => Array(
[ID] => 2
[Fruit] => This is the second description 2
[Price] => 50
)
[4] => Array(
[ID] => 3
[Fruit] => This is the third description
[Price] => 99
)
我用过
$newArray = array_merge_recursive($firstArray, $secondArray);
这只将第二个数组中的条目追加到第一个数组的末尾,理想情况下,我希望新数组如下所示
[0] => Array(
[ID] => 1
[Fruit] => Apple
[State] => Ohio
[description]
Array(
[0] => This is sample description
[1] => This is sample description 2
)
[price]
Array(
[0] => 20
[1] => 15
)
[1] => Array(
[ID] => 1
[Fruit] => Apple
[State] => Ohio
[description]
Array(
[0] => This is the second description
[1] => This is the second description 2
)
[price]
Array(
[0] => 100
[1] => 50
)
基本上,新数组应该在ID上组合,并从第二个数组创建嵌套数组。看看这是否有意义
在这里发布PHP数据构造时,请使用PHP var_export()。这将以PHP可执行代码的形式为我们提供数据。我们可以复制/粘贴到演示原理的代码示例中,而无需将print_r()数据转换为PHP代码的漫长过程!谢谢。我想你不会从一个普通的PHP函数中得到这个。使用两个foreach
循环,它不应该超过几行代码。你的问题不清楚,你到底想要什么?@JagdishChaudhary他希望数组看起来像他发布的数组;)我想问题很清楚。但正如FKEinternet所说,股票功能可能无法实现。