简体   繁体   English

如何在PHP数组中循环遍历数组

[英]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. 我设法获得了第一部分:pageid,step_order,page_url。 I'm having trouble with in_links and out_links and their child arrays. 我在处理in_links和out_links及其子数组时遇到麻烦。 Anyone have ideas on how to pull that data? 有人对如何提取该数据有想法吗?

Here's how you get the domains from in_links. 这是您从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";
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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