【问题标题】:PHP Adding multidimensional arrays togetherPHP将多维数组添加在一起
【发布时间】:2019-05-30 05:23:56
【问题描述】:

我一直想知道如何将 2 个多维数组加在一起,我找到了类似的解决方案,但这并不是我想要的。也许你们中的一个可以帮助我。是的,我知道标题与其他问题几乎相同,但请相信我,我一直在寻找答案,但找不到。

# array1
Array
(
    [0] => Array
        (
            [0] => Product1 
            [1] => Description product 1          
        )

    [1] => Array
        (
            [0] => Product2
            [1] => Description product 2       
        )

    [2] => Array
        (
            [0] => Product3
            [1] => Description product 3       
        )
)

# array2
Array
(
    [0] => Array
        (
            [0] => Price 1
            [1] => Something product 1        
        )

    [1] => Array
        (
            [0] => Price 2
            [1] => Something product 2    
        )

    [2] => Array
        (
            [0] => Price 3
            [1] => Something product 3      
        )
)

#resultant array
Array
(
    [0] => Array
        (
            [0] => Product1 
            [1] => Description product 1
            [3] => Price 1
            [4] => Something product 1
        )

    [1] => Array
        (
            [0] => Product2
            [1] => Description product 2
            [2] => Price 2
            [3] => Something product 2      
        )

    [2] => Array
        (
            [0] => Product3
            [1] => Description product 3  
            [2] => Price 3
            [3] => Something product 3   
        )
)

如您所见,我想将 2 个数组加在一起。我已经看到了其他几个答案,但他们使用内置 php 函数array_merge()。如果我使用它,它会导致这样的结果:

#resultant array
Array
(
    [0] => Array
        (
            [0] => Product1 
            [1] => Description product 1
        )

    [1] => Array
        (
            [0] => Product2
            [1] => Description product 2     
        )

    [2] => Array
        (
            [0] => Product3
            [1] => Description product 3   
        )
    [3] => Array
        (
            [0] => Price 1
            [1] => Something product 1        
        )

    [4] => Array
        (
            [0] => Price 2
            [1] => Something product 2    
        )

    [5] => Array
        (
            [0] => Price 3
            [1] => Something product 3      
        )
)
)

如您所见,不幸的是,这不是我想要的。我希望为我的问题找到解决方案。

感谢您阅读我的帖子。

干杯科迪

【问题讨论】:

  • 写你的owm函数,很简单。

标签: php arrays merge append


【解决方案1】:

你可以这样做

$final = [];
foreach($arr1 as $key => $value){
// loop over the second array elements
 foreach($arr2[$key] as $key2 => $value2){
 // append the second array values to the first array
   $value[] = $value2;
 }
// append the new array to the final array
$final[] = $value;
}

【讨论】:

  • 这似乎可行,但 array_map 效果更好,因为它是 PHP 的内置函数,编写的代码更少,再次感谢您的回答。
【解决方案2】:

您可以使用array_maparray_merge 应用于您的每个子数组:

$result = array_map('array_merge', $array1, $array2);

有关更多信息,请查看array_map 的手册,尤其是示例 3。

【讨论】:

  • 感谢这对我帮助很大,我不知道 array_map 对我的特定问题有很好的用途。再次感谢!
猜你喜欢
  • 1970-01-01
  • 2011-02-10
  • 1970-01-01
  • 2017-12-22
  • 2021-12-29
  • 2011-01-07
  • 1970-01-01
  • 2019-05-30
  • 1970-01-01
相关资源
最近更新 更多