PHP数组组合最佳方法

发布于 2024-09-25 13:05:59 字数 541 浏览 1 评论 0 原文

给定 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 数组函数可以将它们拼接在一起。

given the following arrays in php, how can i best merge them together

$masterkeys = array('Key1','Key2');

$settings_foo = array(
     array('ID'=>'Key1',
           'Foo_Setting'=>'SomeValue'));

$settings_bar = array(
     array('ID'=>'Key1',
           'Bar_Setting'=>'SomeOtherValue')); 

in the end I need $masterkeys to be the combination of each of the settings_[foo|bar] arrays.

Array( ['Key1'] =  Array('Foo_Setting'=>'SomeValue','Bar_Setting'=>'SomeOtherValue') );

I do know I can use a couple foreach loops on this, but just wondering if there are a couple PHP array functions that can splice them together.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖微凉心微凉 2024-10-02 13:05:59

虽然您可以使用 PHP 的一些数组函数,但您的输入数据的格式不是很好。通过自己编写它们,您将获得最少的迭代(并且可能获得最好的性能):

# create an array of $masterkey => array()
$result = array_combine($masterkeys, array_fill(0, count($masterkeys), array()));

# loop through each settings array
foreach (array($settings_foo, $settings_bar) as $settings)
{
  foreach ($settings as $s)
  {
    # merge the array only if the ID is in the master list
    $key = $s['ID'];
    if (array_key_exists($key, $result))    
      $result[$key] = array_merge($result[$key], $s);
  }
}

# unset all extraneous 'ID' properties
foreach (array_keys($result) as $i)
  unset($result[$i]['ID']);

var_dump($result);

作为替代方案,您可以查看 array_maparray_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:

# create an array of $masterkey => array()
$result = array_combine($masterkeys, array_fill(0, count($masterkeys), array()));

# loop through each settings array
foreach (array($settings_foo, $settings_bar) as $settings)
{
  foreach ($settings as $s)
  {
    # merge the array only if the ID is in the master list
    $key = $s['ID'];
    if (array_key_exists($key, $result))    
      $result[$key] = array_merge($result[$key], $s);
  }
}

# unset all extraneous 'ID' properties
foreach (array_keys($result) as $i)
  unset($result[$i]['ID']);

var_dump($result);

As an alternative, you could look into array_map and array_filter, but due to the way the data is structured, I'm not sure they'll be of much use.

不知在何时 2024-10-02 13:05:59

我不确定你的 $masterkeys 数组在这里如何发挥作用,但是 array_merge_recursive 可能会做你想做的事。

I'm not sure how your $masterkeys array plays in here, but array_merge_recursive may do what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文