在 PHP 中将数组与相应的键组合起来

发布于 2024-11-09 05:15:52 字数 1090 浏览 0 评论 0原文

通过匹配每个数组中键中的值来组合两个数组的最佳方法是什么?例如,我有两个数组:

    Array
    (
        [id]  => 1
        [name]    => Apple
        [color] => Green
    )
    (
        [id]  => 2
        [name]    => Banana
        [color] => Yellow
    )
    (
        [id]  => 3
        [name]    => Tomato
        [color] => Red
    )
    Array
    (
        [product_id]  => 1
        [price]    => 0.50
        [weight] => 50
    )
    (
        [product_id]  => 2
        [price]    => 0.99
        [weight] => 80
    )
    (
        [product_id]  => 3
        [price]    => 0.35
        [weight] => 40
)

我想组合 'id' = 'product_id' 来生成:

Array
(
    [id]  => 1
    [name]    => Apple
    [color] => Green
    [price]    => 0.50
    [weight] => 50
)
(
    [id]  => 2
    [name]    => Banana
    [color] => Yellow
    [price]    => 0.99
    [weight] => 80
)
(
    [id]  => 3
    [name]    => Tomato
    [color] => Red
    [price]    => 0.35
    [weight] => 40
)

Whats the best way to combine two arrays by matching values in the keys in each array. For example I have the two arrays:

    Array
    (
        [id]  => 1
        [name]    => Apple
        [color] => Green
    )
    (
        [id]  => 2
        [name]    => Banana
        [color] => Yellow
    )
    (
        [id]  => 3
        [name]    => Tomato
        [color] => Red
    )
    Array
    (
        [product_id]  => 1
        [price]    => 0.50
        [weight] => 50
    )
    (
        [product_id]  => 2
        [price]    => 0.99
        [weight] => 80
    )
    (
        [product_id]  => 3
        [price]    => 0.35
        [weight] => 40
)

And I want to combine where 'id' = 'product_id' to produce:

Array
(
    [id]  => 1
    [name]    => Apple
    [color] => Green
    [price]    => 0.50
    [weight] => 50
)
(
    [id]  => 2
    [name]    => Banana
    [color] => Yellow
    [price]    => 0.99
    [weight] => 80
)
(
    [id]  => 3
    [name]    => Tomato
    [color] => Red
    [price]    => 0.35
    [weight] => 40
)

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

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

发布评论

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

评论(2

与他有关 2024-11-16 05:15:52

您需要编写一个自定义函数来执行此操作。例如:

<?php
function combine_arrays($array1,$array2){
   $merged_array = array();
   for($i=0; $i<count($array1); $i++)
   {
       if($array1[$i]['id'] == $array2[$i]['product_id'])
       {        
           $merged_array[] = array_merge($array1[$i],$array2[$i]);
       }
   }
   return $merged_array;
}
?>

You would need to write a custom function to do this. For example:

<?php
function combine_arrays($array1,$array2){
   $merged_array = array();
   for($i=0; $i<count($array1); $i++)
   {
       if($array1[$i]['id'] == $array2[$i]['product_id'])
       {        
           $merged_array[] = array_merge($array1[$i],$array2[$i]);
       }
   }
   return $merged_array;
}
?>
你的笑 2024-11-16 05:15:52

因此,在这种情况下,将通过添加 id 作为索引来创建两个新数组,如下所示:

$newArray1 = array();
$newArray2 = array();

foreach ($array1 as $key => $value) { $newArray1[$value['id']] = $value; }
foreach ($array2 as $key => $value) { $newArray2[$value['product_id']] = $value; }

之后,很容易合并数组:(

foreach ($array1 as $key => $value)
{
    if (is_array($array1[$key]) && is_array($array2[$key]))
    {
        $result[] = array_merge($array1[$key], $array2[$key]);
    }
}

如果两个数组不包含相同的 id 池,您可能需要添加额外的检查或整体的数量不同)

So, in this case is would create two new arrays by adding the id as index, like this:

$newArray1 = array();
$newArray2 = array();

foreach ($array1 as $key => $value) { $newArray1[$value['id']] = $value; }
foreach ($array2 as $key => $value) { $newArray2[$value['product_id']] = $value; }

After this its easy to merge the arrays:

foreach ($array1 as $key => $value)
{
    if (is_array($array1[$key]) && is_array($array2[$key]))
    {
        $result[] = array_merge($array1[$key], $array2[$key]);
    }
}

(You may have to add additional checks if the two arrays do not contain the same id pool or the amount of entires differs)

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