如何在多维数组的末尾插入数组?

时间:2016-04-04 05:35:01

标签: php json magento

我正在解码一个json数组以插入一个新的数组元素,其中title是" Qty"。之后我需要在json中再次编码它。                 这是来自json decode的数组。

Array
(
    [0] => 
    [1] => stdClass Object
        (
            [order] => 1
            [cols] => 1
            [rows] => 5
            [removable] => 1
            [title] => 
            [fields] => Array
                (
                    [0] => 
                    [1] => stdClass Object
                        (
                            [id] => 0
                            [item_count] => 1
                            [option_id] => 0
                            [title] => Paper
                            [type] => drop_down
                            [is_require] => 0
                            [order] => 1
                            [items] => Array
                                (
                                    [0] => 
                                    [1] => stdClass Object
                                        (
                                            [item_count] => 309
                                            [option_id] => 77
                                            [option_type_id] => -1
                                            [title] => 14 Pt C2S
                                            [price] => 0.00
                                            [price_type] => fixed
                                            [sku] => 
                                            [order] => 1
                                            [image_src] => 
                                            [is_selected] => 0
                                            [is_disabled] => 0
                                            [carriage_return] => 
                                            [css_class] => 
                                            [customer_group] => 
                                            [visibility_condition] => 
                                            [visibility_action] => hidden
                                            [visibility] => visible
                                            [sku_is_product_id] => 
                                            [use_qty] => 
                                            [tier_price] => 
                                            [weight] => 
                                            [sort_order] => 1
                                        )

                                )

                            [validation] => 
                            [default_value] => 
                            [hide_on_focus] => 1
                            [comment] => 
                            [css_class] => 
                            [html_args] => 
                            [section] => 
                            [img_src] => 
                            [img_alt] => 
                            [img_title] => 
                            [static_text] => 
                            [section_order] => 1
                            [internal_id] => 1
                            [visibility] => visible
                            [visibility_condition] => 
                            [visibility_action] => hidden
                            [customer_group] => 
                            [default_select_title] => -- Please Select --
                            [itoris_option_id] => 0
                            [sort_order] => 1
                        )

                    [2] => stdClass Object
                        (
                            [id] => 0
                            [item_count] => 3
                            [option_id] => 0
                            [title] => Size
                            [type] => drop_down
                            [is_require] => 0
                            [order] => 2
                            [items] => Array
                                (
                                    [0] => 
                                    [1] => stdClass Object
                                        (
                                            [item_count] => 310
                                            [option_id] => 78
                                            [option_type_id] => -1
                                            [title] => 2 x 7
                                            [price] => 0.00
                                            [price_type] => fixed
                                            [sku] => 
                                            [order] => 1
                                            [image_src] => 
                                            [is_selected] => 0
                                            [is_disabled] => 0
                                            [carriage_return] => 
                                            [css_class] => 
                                            [customer_group] => 
                                            [visibility_condition] => 
                                            [visibility_action] => hidden
                                            [visibility] => visible
                                            [sku_is_product_id] => 
                                            [use_qty] => 
                                            [tier_price] => 
                                            [weight] => 
                                            [sort_order] => 1
                                        )
                               )

                            [validation] => 
                            [default_value] => 
                            [hide_on_focus] => 1
                            [comment] => 
                            [css_class] => 
                            [html_args] => 
                            [section] => 
                            [img_src] => 
                            [img_alt] => 
                            [img_title] => 
                            [static_text] => 
                            [section_order] => 1
                            [internal_id] => 2
                            [visibility] => visible
                            [visibility_condition] => 
                            [visibility_action] => hidden
                            [customer_group] => 
                            [default_select_title] => -- Please Select --
                            [itoris_option_id] => 0
                            [sort_order] => 2
                        )

                        [3] => stdClass Object
                        (
                            [id] => 0
                            [item_count] => 3
                            [option_id] => 0
                            [title] => Qty
                            [type] => drop_down
                            [is_require] => 0
                            [order] => 2
                            [items] => Array
                                (
                                    [0] => 
                                    [1] => stdClass Object
                                        (
                                            [item_count] => 312
                                            [option_id] => 81
                                            [option_type_id] => -1
                                            [title] => 500
                                            [price] => 20.00
                                            [price_type] => fixed
                                            [sku] => 
                                            [order] => 1
                                            [image_src] => 
                                            [is_selected] => 0
                                            [is_disabled] => 0
                                            [carriage_return] => 
                                            [css_class] => 
                                            [customer_group] => 
                                            [visibility_condition] => 
                                            [visibility_action] => hidden
                                            [visibility] => visible
                                            [sku_is_product_id] => 
                                            [use_qty] => 
                                            [tier_price] => 
                                            [weight] => 
                                            [sort_order] => 1
                                        )
                                        [2] => stdClass Object
                                        (
                                            [item_count] => 313
                                            [option_id] => 81
                                            [option_type_id] => -1
                                            [title] => 600
                                            [price] => 50.00
                                            [price_type] => fixed
                                            [sku] => 
                                            [order] => 1
                                            [image_src] => 
                                            [is_selected] => 0
                                            [is_disabled] => 0
                                            [carriage_return] => 
                                            [css_class] => 
                                            [customer_group] => 
                                            [visibility_condition] => 
                                            [visibility_action] => hidden
                                            [visibility] => visible
                                            [sku_is_product_id] => 
                                            [use_qty] => 
                                            [tier_price] => 
                                            [weight] => 
                                            [sort_order] => 1
                                        )
                               )

                            [validation] => 
                            [default_value] => 
                            [hide_on_focus] => 1
                            [comment] => 
                            [css_class] => 
                            [html_args] => 
                            [section] => 
                            [img_src] => 
                            [img_alt] => 
                            [img_title] => 
                            [static_text] => 
                            [section_order] => 1
                            [internal_id] => 2
                            [visibility] => visible
                            [visibility_condition] => 
                            [visibility_action] => hidden
                            [customer_group] => 
                            [default_select_title] => -- Please Select --
                            [itoris_option_id] => 0
                            [sort_order] => 2
                        )

        [moveUp] => stdClass Object
                (
                    [_prototypeUID] => 108
                )

            [delimiter] => Array
                (
                )

            [moveDown] => stdClass Object
                (
                    [_prototypeUID] => 109
                )

            [visibility_action] => hidden
            [visibility] => visible
        )

)       

)   

这是我需要添加的新数组元素。

[3] => stdClass Object
       (
           [item_count] => 314
           [option_id] => 81
           [option_type_id] => -1
           [title] => 444
           [price] => 30.00
           [price_type] => fixed
           [sku] => 
           [order] => 1
           [image_src] => 
           [is_selected] => 0
           [is_disabled] => 0
           [carriage_return] => 
           [css_class] => 
           [customer_group] => 
           [visibility_condition] => 
           [visibility_action] => hidden
           [visibility] => visible
           [sku_is_product_id] => 
           [use_qty] => 
           [tier_price] => 
           [weight] => 
           [sort_order] => 1
      )         

这是我的代码。

$configurationKeys = array('item_count'=>'314', 'option_id'=>"81", 'option_type_id'=>"0", 'title'=>"444", 'price'=>"30.00", 'price_type'=>"", 'order'=>"", 'sku'=>"",'order'=>'testorder', 'image_src'=>"visible", 'is_selected'=>"hidden", 'is_disabled'=>"", 'carriage_return'=>"0", 'css_class'=>"", 'customer_group'=>"", 'visibility_condition'=>"", 'visibility_action'=>"", 'visibility'=>"", 'sku_is_product_id'=>""
                            );

                            while($row = $itorisOptions->fetch())
                            {
                                $jsondecode = json_decode($row['configuration']);


                            foreach($jsondecode as $k=>$code)
                        {
                            foreach($code as $mykey=>$mycode)
                            {

                                foreach($mycode as $mykey1=>$mycode1)
                                {

                                    if($mycode1->title=="Qty") 
                                    {
                                        //$jsondecode->$k->$mykey->$mykey1->items[]=(object)$configurationKeys;     

                                        $item_count = count($mycode1->items); 
                                        echo $jsondecode->$k->$mykey->$mykey1->items[$item_count+1]=(object)$configurationKey‌​s;



                                    }


                                }

                            }


                        }
                          print_r($jsondecode);
                            }

1 个答案:

答案 0 :(得分:0)

而不是 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <mvc:annotation-driven/> <context:component-scan base-package="com.mytcg.controller"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name= "prefix" value="/WEB-INF/jsp/"/> <property name= "suffix" value=".jsp"/> </bean> <!--configuring HandlerMapping --> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> </beans>

用于()或foreach()循环&amp;增加价值如:

array_push($codeval,$configurationKeys);
           $test[$k][$mykey][$mykey1][$v] = $codeval;