PHP从嵌套数组中提取键值

时间:2017-01-13 21:21:56

标签: php arrays nested

我有一个API调用,返回以下数组。我在PHP中尝试做的只是获取[by_category]数组中对象的关键值。我想只显示该数组的类别名称和视图的值。但我仍然坚持如何遍历这些嵌套数组以获取那些值,忽略[by_date]数组中的所有内容。有关从哪里开始的任何提示?我可以使用的任何示例代码?

Array
(
    [by_date] => Array
     (
        [0] => Array
            (
                [id] => 
                [date] => 2016-11-10
                [views] => 1202
                [participations] => 29
            )

        [1] => Array
            (
                [id] => 
                [date] => 2016-09-10
                [views] => 2068
                [participations] => 66
            )

        [2] => Array
            (
                [id] => 
                [date] => 2016-11-14
                [views] => 1760
                [participations] => 41
            )

        [3] => Array
            (
                [id] => 
                [date] => 2017-01-01
                [views] => 122
                [participations] => 3
            )

        [4] => Array
            (
                [id] => 
                [date] => 2016-10-06
                [views] => 2113
                [participations] => 102
            )

        [5] => Array
            (
                [id] => 
                [date] => 2016-08-31
                [views] => 1986
                [participations] => 110
            )

        [6] => Array
            (
                [id] => 
                [date] => 2016-07-26
                [views] => 127
                [participations] => 3
            )

        [7] => Array
            (
                [id] => 
                [date] => 2016-12-03
                [views] => 826
                [participations] => 24
            )

        [8] => Array
            (
                [id] => 
                [date] => 2016-11-13
                [views] => 796
                [participations] => 20
            )

    )

[by_category] => Array
    (
        [0] => Array
            (
                [id] => 
                [category] => announcements
                [views] => 2639
            )

        [1] => Array
            (
                [id] => 
                [category] => assignments
                [views] => 21611
            )

        [2] => Array
            (
                [id] => 
                [category] => collaborations
                [views] => 6
            )

        [3] => Array
            (
                [id] => 
                [category] => conferences
                [views] => 6
            )

        [4] => Array
            (
                [id] => 
                [category] => discussions
                [views] => 29962
            )

        [5] => Array
            (
                [id] => 
                [category] => files
                [views] => 13649
            )

        [6] => Array
            (
                [id] => 
                [category] => general
                [views] => 23497
            )

        [7] => Array
            (
                [id] => 
                [category] => grades
                [views] => 5473
            )

        [8] => Array
            (
                [id] => 
                [category] => groups
                [views] => 231
            )

        [9] => Array
            (
                [id] => 
                [category] => modules
                [views] => 27258
            )

        [10] => Array
            (
                [id] => 
                [category] => other
                [views] => 18186
            )

        [11] => Array
            (
                [id] => 
                [category] => pages
                [views] => 16576
            )

        [12] => Array
            (
                [id] => 
                [category] => quizzes
                [views] => 6206
            )

    )

)

1 个答案:

答案 0 :(得分:0)

通过foreach很容易:

foreach($array['by_category'] as $category){
    echo 'id='. $category['id'] . ' , name='.$category['category'];
}