如何替换子数组

时间:2017-06-28 15:29:51

标签: php arrays json

我在我的项目中获得了json然后我已经替换了它的一些子数组并再次必须通过它。所以,当我解码json并用pre标签显示它时,就像这样显示。

Array
(
[callid] => 59538ea658dfa
[action] => config_get
[message] => ok
[data] => Array
    (
        [config_file] => /cf/conf/config.xml
        [config] => Array
            (
                [filter] => Array
                    (
                        [separator] => Array
                            (
                                [wan] => 
                            )

                        [rule] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 
                                        [tracker] => 1498479412
                                        [type] => pass
                                        [interface] => wan
                                        [ipprotocol] => inet
                                        [tag] => 
                                        [tagged] => 
                                        [max] => 
                                        [max-src-nodes] => 
                                        [max-src-conn] => 
                                        [max-src-states] => 
                                        [statetimeout] => 
                                        [statetype] => keep state
                                        [os] => 
                                        [protocol] => tcp
                                        [source] => Array
                                            (
                                                [any] => 
                                            )

                                        [destination] => Array
                                            (
                                                [any] => 
                                            )

                                        [descr] => Hi manoj2
                                        [updated] => Array
                                            (
                                                [time] => 1498479412
                                                [username] => admin@172.16.1.22
                                            )

                                        [created] => Array
                                            (
                                                [time] => 1498479412
                                                [username] => admin@172.16.1.22
                                            )

                                    )

                                [1] => Array
                                    (
                                        [statetype] => keep state
                                        [statetimeout] => 
                                        [max-src-states] => 
                                        [max] => 
                                        [tagged] => 
                                        [tag] => 
                                        [ipprotocol] => inet
                                        [interface] => wan
                                        [os] => 
                                        [source] => Array
                                            (
                                                [any] => 
                                            )

                                        [destination] => Array
                                            (
                                                [any] => 
                                            )

                                        [descr] => hi Divyesh
                                        [updated] => Array
                                            (
                                                [username] => admin@192.168.1.100
                                                [time] => 1498301528
                                            )

                                        [created] => Array
                                            (
                                                [username] => admin@192.168.1.100
                                                [time] => 1498301528
                                            )

                                        [type] => pass
                                        [tracker] => 1498301528
                                        [id] => 
                                        [max-src-nodes] => 
                                        [max-src-conn] => 
                                    )

                                [2] => Array
                                    (
                                        [destination] => Array
                                            (
                                                [any] => 
                                            )

                                        [source] => Array
                                            (
                                                [network] => lan
                                            )

                                        [tracker] => 0100000101
                                        [interface] => lan
                                        [descr] => Default allow LAN to any rule
                                        [ipprotocol] => inet
                                        [type] => pass
                                    )

                                [3] => Array
                                    (
                                        [destination] => Array
                                            (
                                                [any] => 
                                            )

                                        [source] => Array
                                            (
                                                [network] => lan
                                            )

                                        [tracker] => 0100000102
                                        [interface] => lan
                                        [descr] => Default allow LAN IPv6 to any rule
                                        [ipprotocol] => inet6
                                        [type] => pass
                                    )

                            )

                    )

                [shaper] => 
                [ipsec] => 
                [aliases] => Array
                    (
                        [alias] => Array
                            (
                                [0] => Array
                                    (
                                        [name] => manoj2_alias2
                                        [type] => network
                                        [address] => manoj2_alias3 172.30.2.65/16 172.30.2.51/32
                                        [descr] => descc
                                        [detail] => nfvbg||dfg||asd | asdfg
                                    )

                                [1] => Array
                                    (
                                        [name] => manoj2_alias3
                                        [type] => host
                                        [address] => tyuty tyu
                                        [descr] => dess
                                        [detail] => tyu||tyu
                                    )

                                [2] => Array
                                    (
                                        [name] => sdf
                                        [type] => network
                                        [address] => manoj2_alias2
                                        [descr] => afsasf || weriojdg
                                        [detail] => 256418
                                    )

                            )

                    )

            )

    )

我想用 [规则] 替换部分子数组。

[rule] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 
                                        [tracker] => 1498479412
                                        [type] => block
                                        [interface] => lan
                                        [ipprotocol] => any
                                        [tag] => 
                                        [tagged] => 
                                        [max] => 
                                        [max-src-nodes] => 
                                        [max-src-conn] => 
                                        [max-src-states] => 
                                        [statetimeout] => 
                                        [statetype] => keep state
                                        [os] => 
                                        [protocol] => tcp/udp
                                        [source] => Array
                                            (
                                                [any] => 
                                            )

                                        [destination] => Array
                                            (
                                                [any] => 
                                            )

                                        [descr] => second option
                                        [updated] => Array
                                            (
                                                [time] => 149844356
                                                [username] => admin@172.16.1.22
                                            )

                                        [created] => Array
                                            (
                                                [time] => 1498479412
                                                [username] => admin@172.16.1.22
                                            )

                                    )

                                [1] => Array
                                    (
                                        [statetype] => keep state
                                        [statetimeout] => 
                                        [max-src-states] => 
                                        [max] => 
                                        [tagged] => 
                                        [tag] => 
                                        [ipprotocol] => single
                                        [interface] => wan
                                        [os] => 
                                        [source] => Array
                                            (
                                                [any] => 172.31.2.65/24
                                            )

                                        [destination] => Array
                                            (
                                                [any] => manoj2_alias2
                                            )

                                        [descr] => first option
                                        [updated] => Array
                                            (
                                                [username] => admin@192.168.1.100
                                                [time] => 1498301528
                                            )

                                        [created] => Array
                                            (
                                                [username] => admin@192.168.1.100
                                                [time] => 1498301528
                                            )

                                        [type] => pass
                                        [tracker] => 1498301528
                                        [id] => 
                                        [max-src-nodes] => 
                                        [max-src-conn] => 
                                    )

                            )

怎么做请帮帮我。我试过但没有任何成功。

1 个答案:

答案 0 :(得分:0)

访问您的数组元素并将新数组分配给它。

$array['data']['config']['filter']['rule'] = $theNewRuleArray;