解析具有多个嵌套的PHP数组

时间:2014-07-15 10:54:17

标签: php

我是一个新手,正在寻找解析PHP数组的帮助。我已经生成了以下数组,它保存在:

$items = $pocket->retrieve($params, $accessToken);

如您所见,每个项目的状态属性为0或1.我正在寻找一种简单的方法来遍历每个项目,并找出有多少状态为0,有多少状态为1。 / p>

我试过使用一个简单的foreach但没有成功。问题是,没有返回任何内容,所以我认为我没有正确解析数组。

如果我做一个简单的print_r,它会打印出数组。

print_r($items); 

到目前为止我所拥有的:

$items = $pocket->retrieve($params, $accessToken);
$hasRead = 0;
$hasNotRead = 0;

foreach ($items as $key) {

    if ($key["status"] == 1) {
        $hasRead++;
    }
    else {
        $hasNotRead++;
    }

    echo "Read = " . $hasRead;
    echo "Not Rad = " . $hasNotRead;

}

任何帮助非常感谢!

Array
(
    [status] => 1
    [complete] => 1
    [list] => Array
        (
            [666040191] => Array
                (
                    [item_id] => 666040191
                    [resolved_id] => 666040191
                    [given_url] => https://medium.com/matter/my-life-with-piper-from-big-house-to-small-screen-592b35f5af94
                    [given_title] => The Other True Story Behind ‘Orange Is The New Black’
                    [favorite] => 0
                    [status] => 0
                    [time_added] => 1405415236
                    [time_updated] => 1405415236
                    [time_read] => 0
                    [time_favorited] => 0
                    [sort_id] => 0
                    [resolved_title] => My Life with Piper: From Big House to Small Screen
                    [resolved_url] => https://medium.com/matter/my-life-with-piper-from-big-house-to-small-screen-592b35f5af94
                    [excerpt] => I was 29 years old and living the dream, or at least my version of it, when everything changed. I was in love with an amazing woman and had a rent-controlled sublet in New York City’s West Village and a good job at a glossy magazine.
                    [is_article] => 1
                    [is_index] => 0
                    [has_video] => 0
                    [has_image] => 1
                    [word_count] => 10066
                )

            [665694007] => Array
                (
                    [item_id] => 665694007
                    [resolved_id] => 665694007
                    [given_url] => http://digg.com/video/weird-al-yankovic-parodies-pharrells-happy
                    [given_title] => http://digg.com/video/weird-al-yankovic-parodies-pharrells-happy
                    [favorite] => 0
                    [status] => 0
                    [time_added] => 1405415180
                    [time_updated] => 1405415180
                    [time_read] => 0
                    [time_favorited] => 0
                    [sort_id] => 1
                    [resolved_title] => Weird Al Yankovic Parodies Pharrell's 'Happy'
                    [resolved_url] => http://digg.com/video/weird-al-yankovic-parodies-pharrells-happy
                    [excerpt] => Weird Al does a one-shot spoof of Pharrell's ubiquitous "Happy" with some surprise help from a few celebrities.
                    [is_article] => 1
                    [is_index] => 0
                    [has_video] => 0
                    [has_image] => 0
                    [word_count] => 18
                )

            [664691248] => Array
                (
                    [item_id] => 664691248
                    [resolved_id] => 664691252
                    [given_url] => http://www.thedailybeast.com/articles/2014/07/13/an-investigation-into-the-delicious-origins-of-ice-cream.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+thedailybeast%2Farticles+%28The+Daily+Beast+-+Latest+Articles%29
                    [given_title] => http://www.thedailybeast.com/articles/2014/07/13/an-investigation-into-the-
                    [favorite] => 0
                    [status] => 0
                    [time_added] => 1405415169
                    [time_updated] => 1405415169
                    [time_read] => 0
                    [time_favorited] => 0
                    [sort_id] => 2
                    [resolved_title] => An Investigation Into the Delicious Origins of Ice Cream
                    [resolved_url] => http://www.thedailybeast.com/articles/2014/07/13/an-investigation-into-the-delicious-origins-of-ice-cream.html
                    [excerpt] => Thirty years ago this week, Ronald Reagan made perhaps the most momentous decision of his presidency. "Ice cream is a nutritious and wholesome food," he declared on July 9, 1984. "It enjoys a reputation as the perfect dessert and snack."
                    [is_article] => 1
                    [is_index] => 0
                    [has_video] => 0
                    [has_image] => 0
                    [word_count] => 1262
                )

        )

    [error] => 
    [search_meta] => Array
        (
            [search_type] => normal
        )

    [since] => 1405421539
)

1 个答案:

答案 0 :(得分:1)

我想你想迭代 list 数组吗?

$items = $pocket->retrieve($params, $accessToken);
$hasRead = 0;
$hasNotRead = 0;

foreach ($items['list'] as $key) { //Added list

    if ($key["status"] == 1) {
        $hasRead++;
    }
    else {
        $hasNotRead++;
    }

}

echo "Read = " . $hasRead;
echo "Not Rad = " . $hasNotRead;