douren0558 2013-03-14 17:35
浏览 41
已采纳

如何在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?

  • 写回答

1条回答 默认 最新

  • doolo00026 2013-03-14 17:40
    关注

    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
    ";
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗