本文介绍了合并两个数组元素融入在PHP中一个数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想两个数组合并成一个阵列如下,

数组1:

 阵列

    [0] =>排列
        (
            [ID] => 3
            [SKU] => KOG456
            [cart_id] => 2
            [名] =>杨钱
            [塞] =>年轻的钱
            [ROUTE_ID] => 47
            [说明] =>
这是测试的歌
            [摘录] =>
            [saleprice] => 90.00
            [related_products] =>
            [图片] => {\"1c6b0883fc94c5f644497ec488cdf8cb\":{\"filename\":\"1c6b0883fc94c5f644497ec488cdf8cb.jpg\",\"alt\":\"Test\",\"caption\":\"\",\"primary\":true}}
            [seo_title] =>
            [元] =>
            [启用] => 1
        ))

ARRAY2:

 阵列

    [0] =>排列
        (
            [文件名] => Beethovens_Symphony_No._9_(谐谑曲).WMA
            [标题] =>杨钱
            [大小] = GT; 599.26
        ))

预期阵列的结果是:

 阵列

    [0] =>排列
        (
            [ID] => 3
            [SKU] => KOG456
            [cart_id] => 2
            [名] =>杨钱
            [塞] =>年轻的钱
            [ROUTE_ID] => 47
            [说明] =>
这是测试的歌
            [摘录] =>
            [saleprice] => 90.00
            [related_products] =>
            [图片] => {\"1c6b0883fc94c5f644497ec488cdf8cb\":{\"filename\":\"1c6b0883fc94c5f644497ec488cdf8cb.jpg\",\"alt\":\"Test\",\"caption\":\"\",\"primary\":true}}
            [seo_title] =>
            [元] =>
            [启用] => 1
            [文件名] => Beethovens_Symphony_No._9_(谐谑曲).WMA
            [标题] =>杨钱
            [大小] = GT; 599.26
        ))

如何将这些数组元素合并到一个数组元素?


解决方案

 的foreach($ origArray为$关键=>&安培; $子阵列)
    $子阵+ = $ arrayToBeAdded [$关键];

其中, $ origArray 是你的阵列将被合并到和 $ arrayToBeAdded 您合并入阵。

I want to merge two arrays into one array as follows,

Array1:

Array
(
    [0] => Array
        (
            [id] => 3
            [sku] => KOG456
            [cart_id] => 2
            [name] => Young Money
            [slug] => young-money
            [route_id] => 47
            [description] =>
This is test song


            [excerpt] =>
            [saleprice] => 90.00
            [related_products] =>
            [images] => {"1c6b0883fc94c5f644497ec488cdf8cb":{"filename":"1c6b0883fc94c5f644497ec488cdf8cb.jpg","alt":"Test","caption":"","primary":true}}
            [seo_title] =>
            [meta] =>
            [enabled] => 1
        )

)

Array2:

Array
(
    [0] => Array
        (
            [filename] => Beethovens_Symphony_No._9_(Scherzo).wma
            [title] => Young Money
            [size] => 599.26
        )

)

Expected array result is:

Array
(
    [0] => Array
        (
            [id] => 3
            [sku] => KOG456
            [cart_id] => 2
            [name] => Young Money
            [slug] => young-money
            [route_id] => 47
            [description] =>
This is test song


            [excerpt] =>
            [saleprice] => 90.00
            [related_products] =>
            [images] => {"1c6b0883fc94c5f644497ec488cdf8cb":{"filename":"1c6b0883fc94c5f644497ec488cdf8cb.jpg","alt":"Test","caption":"","primary":true}}
            [seo_title] =>
            [meta] =>
            [enabled] => 1
            [filename] => Beethovens_Symphony_No._9_(Scherzo).wma
            [title] => Young Money
            [size] => 599.26
        )

)

How to merge these array elements into one array element ?

解决方案
foreach ($origArray as $key => &$subArray)
    $subArray += $arrayToBeAdded[$key];

Where $origArray is your array which is to be merged into and $arrayToBeAdded the array you merge into.

这篇关于合并两个数组元素融入在PHP中一个数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 16:28