简体   繁体   中英

PHP loop through array and split into seperate arrays

Hi I have this PHP array of Amazon Sub Categories what I am trying to do is loop through the array and split into 3 seperate ones,

Heres the Array, and needs to be split into 3 arrays $request, and all the children under it $subCats and final variable $Ancestors. The $subCats will contain all the [Children][BrowseNode]. Its driving me mad I cant seem to loop through correctly.

Any thoughts people. Cheers in advanced.

J.

Array
(
    [Request] => Array
        (
            [IsValid] => True
            [BrowseNodeLookupRequest] => Array
                (
                    [BrowseNodeId] => 66471031
                    [ResponseGroup] => Array
                        (
                            [0] => BrowseNodeInfo
                        )

                )

        )

    [BrowseNode] => Array
        (
            [BrowseNodeId] => 66471031
            [Name] => Beauty
            [Children] => Array
                (
                    [BrowseNode] => Array
                        (
                            [0] => Array
                                (
                                    [BrowseNodeId] => 118423031
                                    [Name] => Cosmetics
                                )

                            [1] => Array
                                (
                                    [BrowseNodeId] => 74006031
                                    [Name] => Cosmetics_hidden
                                )

                            [2] => Array
                                (
                                    [BrowseNodeId] => 118457031
                                    [Name] => Fragrances
                                )

                            [3] => Array
                                (
                                    [BrowseNodeId] => 66466031
                                    [Name] => Fragrances_hidden
                                )


                        )

                )

            [Ancestors] => Array
                (
                    [BrowseNode] => Array
                        (
                            [BrowseNodeId] => 66280031
                            [Name] => Categories
                            [IsCategoryRoot] => 1
                            [Ancestors] => Array
                                (
                                    [BrowseNode] => Array
                                        (
                                            [BrowseNodeId] => 65801031
                                            [Name] => Health & Beauty
                                        )

                                )

                        )

                )

        )

)

This should do it:

$request = your_main_array['request'];
$subCats = your_main_array['BrowseNode']['Children']['BrowseNode'];
$Ancestors = your_main_array['BrowseNode']['Ancestors']

['Children']['BrowseNode'] and ['Ancestors'] are in a [BrowseNode] Array under you main array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM