PHP 将关联数组 ID 值替换为另一个数组中的名称值
可能的重复:
如何合并 PHP 数组?
我有两个数组,都是 db 的结果查询。我在下面有一个简单的例子(没有真实数据 - 只是用于演示目的。真实数据要复杂得多)。
$results:
Array
( [0] =>
Array ( [id] => 20 [age] => 29 )
[1] =>
Array ( [id] => 593 [age] => 38 )
)
$persons:
Array
( [0] =>
Array ( [id] => 593 [name] => Jack Jones )
[1] =>
Array ( [id] => 20 [name] => John Smith )
)
我的问题是:如何匹配 $persons[name] 来替换 $results[id] ,这样我最终会得到:
$results:
Array
( [0] =>
Array ( [id] => John Smith [age] => 29 )
[1] =>
Array ( [id] => Jack Jones [age] => 38 )
)
数组是无序的 - 如果键匹配,我需要替换值(是的, $ 中的每个键)结果在 $persons 中肯定有相应的条目)。非常感谢任何帮助!
Possible Duplicate:
How can I merge PHP arrays?
I have two arrays, both results of db queries. I have a simple example below (no the real data- just for demo purposes. The real data is significantly more complex).
$results:
Array
( [0] =>
Array ( [id] => 20 [age] => 29 )
[1] =>
Array ( [id] => 593 [age] => 38 )
)
$persons:
Array
( [0] =>
Array ( [id] => 593 [name] => Jack Jones )
[1] =>
Array ( [id] => 20 [name] => John Smith )
)
My question is: how can I match the $persons[name] to replace $results[id] so that I end up with:
$results:
Array
( [0] =>
Array ( [id] => John Smith [age] => 29 )
[1] =>
Array ( [id] => Jack Jones [age] => 38 )
)
the arrays are unorderd - I need to replace values if the keys match (and yes, each key in $results definitely has a corresponding entry in $persons). Any help much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)