PHP数组组合最佳方法
给定 php 中的以下数组,我怎样才能最好地将它们合并在一起
$masterkeys = array('Key1','Key2');
$settings_foo = array(
array('ID'=>'Key1',
'Foo_Setting'=>'SomeValue'));
$settings_bar = array(
array('ID'=>'Key1',
'Bar_Setting'=>'SomeOtherValue'));
,最后我需要 $masterkeys 作为每个 settings_[foo|bar] 数组的组合。
Array( ['Key1'] = Array('Foo_Setting'=>'SomeValue','Bar_Setting'=>'SomeOtherValue') );
我确实知道我可以对此使用几个 foreach 循环,但只是想知道是否有几个 PHP 数组函数可以将它们拼接在一起。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然您可以使用 PHP 的一些数组函数,但您的输入数据的格式不是很好。通过自己编写它们,您将获得最少的迭代(并且可能获得最好的性能):
作为替代方案,您可以查看
array_map
和array_filter
,但由于数据是结构化的,我不确定它们会有多大用处。While you can use some of PHP's array functions, your input data isn't in a very nice format. You'll have the fewest iterations (and probably best performance) by writing them yourself:
As an alternative, you could look into
array_map
andarray_filter
, but due to the way the data is structured, I'm not sure they'll be of much use.我不确定你的
$masterkeys
数组在这里如何发挥作用,但是 array_merge_recursive 可能会做你想做的事。I'm not sure how your
$masterkeys
array plays in here, but array_merge_recursive may do what you want.