如何在php中循环数组中的数组

时间:2013-03-14 17:35:58

标签: php arrays for-loop foreach nested-loops

这是数组结构(只是数组中的第一个元素):

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
        )
)

我正在尝试循环以获取域,展示(和子元素)中的元素。我设法获得了第一部分:pageid,step_order,page_url。我在使用in_links和out_links及其子数组时遇到问题。任何人都有关于如何提取数据的想法?

1 个答案:

答案 0 :(得分:1)

以下是从in_links获取域名的方法。其他类似(但我不确定展示子数组中的索引代表什么)。

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