简体   繁体   中英

how to loop through an array within an array in php

Here's the array structure (only the first element in the array):

Array
(
    [1] => Array
        (
            [pageid] => 1
            [step_order] => 1
            [pageurl] => http://www.domain.com/
            [in_links] => Array
                (
                    [domains] => Array
                        (
                            [Direct Entry] => 1520
                            [www.google.com] => 387
                            [www.google.co.in] => 14
                            [search.yahoo.com] => 10
                            [All other] => 27
                        )

                    [impressions] => Array
                        (
                            [Direct Entry] => Array
                                (
                                    [0] => 10654
                                    [1] => 10728
                                    [2] => 10772
                                )

                            [www.google.com] => Array
                                (
                                    [0] => 10991
                                    [1] => 12455
                                    [2] => 12466
                                    [3] => 10757
                                )

                            [www.google.co.in] => Array
                                (
                                    [0] => 9839
                                    [1] => 9837
                                    [2] => 9845
                                )

                            [search.yahoo.com] => Array
                                (
                                    [0] => 12087
                                    [1] => 10864
                                )

                        )

                )

            [out_links] => Array
                (
                    [domain] => Array
                        (
                            [Left site] => 1752
                            [http://www.domain.com/#] => 102
                            [http://www.domain.com/contact] => 102
                            [http://www.domain.com/#basic_inline_div] => 2
                        )

                    [impressions] => Array
                        (
                            [Left site] => Array
                                (
                                    [0] => 7680
                                    [1] => 9728
                                    [2] => 10496
                                )

                            [http://www.domain.com/#] => Array
                                (
                                    [0] => 259
                                    [1] => 11013
                                )

                            [http://www.domain.com/contact] => Array
                                (
                                    [0] => 12802
                                    [1] => 10757
                                )

                            [http://www.domain.com/#basic_inline_div] => Array
                                (
                                    [0] => 11
                                    [1] => 51
                                )

                        )

                )

            [visitors] => 1958
        )
)

I'm trying to loop to get the elements from domains, impressions (and sub elements). I managed to get the first parts: pageid, step_order, page_url. I'm having trouble with in_links and out_links and their child arrays. Anyone have ideas on how to pull that data?

Here's how you get the domains from in_links. The others are similar (but I'm not sure what the indexes in the impressions sub-array represent).

foreach ($array as $element) {
  foreach ($element['in_links']['domains'] as $domain => $count) {
    echo "Domain: $domain, Count: $count\n";
  }
}

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