结合两个php数组用于json编码

时间:2015-07-24 07:13:30

标签: arrays json multidimensional-array

目前正在使用CakePhp..i在组合两个数组时遇到了一些问题并准备了一个json编码的数组..我使用了array_merge()属性,但它无法工作..我怎样才能编码这两个数组...

M这样做:

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数组..

1 个答案:

答案 0 :(得分:0)

您必须使用array_merge,但不能直接使用array1array2

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

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

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