合并两个php数组进行json编码

问题描述 投票:0回答:1

目前我正在使用 CakePhp..我在组合两个数组并准备 json 编码数组时遇到了一些问题..我使用了 array_merge() 属性,但它不起作用..我如何对这两个数组进行编码..

我这样做:

return json_encode(array_merge ($product_list,$price_list));

我有两个 php 数组,如下所示:

数组1:

Array
(
    [0] => Array
        (
            [PriceList] => Array
                (
                    [price_id] => 2
                    [price_name] => abc
                    [date_time] => 2015-07-06 16:22:56
                    [dealer_type] => Dealer
                    [purpose] => dealer
                    [status] => ACTIVE
                )

        )

    [1] => Array
        (
            [PriceList] => Array
                (
                    [price_id] => 3
                    [price_name] => xyz
                    [date_time] => 2015-07-06 16:22:56
                    [dealer_type] => Dealer
                    [purpose] => dealer
                    [status] => ACTIVE
                )

        )

)

数组2:

Array
(
    [0] => Array
        (
            [Product] => Array
                (
                    [cat_id] => 1
                    [subcat_id] => 3
                    [brand_id] => 1
                    [p_code] => PP12567
                    [name] => akai
                    [model_no] => 
                    [specification] => color tv
                    [color] => 
                    [quality] => 
                    [size] => 
                    [p_unavail] => 1
                    [demo_avail] => 0
                    [brochure] => 
                    [status] => active
                )

            [ProductPrice] => Array
                (
                    [id] => 154
                    [p_code] => PP12567
                    [price_id] => 1
                    [quantity] => 233
                    [purchase_price] => 344.00
                    [selling_price] => 44.00
                    [discount_price] => 33.00
                    [tax] => 5.00
                    [datetime] => 2015-07-23 15:47:11
                )

            [ProductSubCategory] => Array
                (
                    [subcat_id] => 3
                    [cat_id] => 1
                    [subcat_name] => samsung
                    [status] => active
                )

            [ProductCategory] => Array
                (
                    [cat_id] => 1
                    [cat_name] => Electronics
                    [cat_type] => Product 
                    [status] => active
                )

        )

    [1] => Array
        (
            [Product] => Array
                (
                    [cat_id] => 1
                    [subcat_id] => 4
                    [brand_id] => 1
                    [p_code] => PBC-676767
                    [name] => music
                    [model_no] => 33
                    [specification] => 
                    [color] => 
                    [quality] => 
                    [size] => 
                    [p_unavail] => 0
                    [demo_avail] => 0
                    [brochure] => 
                    [status] => active
                )

            [ProductPrice] => Array
                (
                    [id] => 156
                    [p_code] => PBC-676767
                    [price_id] => 1
                    [quantity] => 767
                    [purchase_price] => 54.00
                    [selling_price] => 55.00
                    [discount_price] => 22.00
                    [tax] => 3.00
                    [datetime] => 2015-07-23 15:47:11
                )

            [ProductSubCategory] => Array
                (
                    [subcat_id] => 4
                    [cat_id] => 1
                    [subcat_name] => sony
                    [status] => active
                )

            [ProductCategory] => Array
                (
                    [cat_id] => 1
                    [cat_name] => Electronics
                    [cat_type] => Product 
                    [status] => active
                )

        )
)

如何将这两个数组合并为一个并编码为 json 数组..

php arrays json multidimensional-array
1个回答
0
投票

您必须使用

array_merge
,但不能直接在
array1
array2
上使用:

$toEncodeArray = [array_merge ($array1[0], $array2[0])] ;

如果您只想要关联数组(没有包装数组),只需执行以下操作:

$toEncodeArray = array_merge ($array1[0], $array2[0]) ;
© www.soinside.com 2019 - 2024. All rights reserved.