2014-07-15 28 views
-1

我是一个新手,正在寻找一些解析PHP数组的帮助。我已经生成以下数组并将其保存在:解析一个带有多个嵌套的PHP数组

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

正如你所看到的,每个项目都有一个状态属性,是0或1。我的每个项目,寻找一种简单的方法来循环并找出多少有状态0和多少有状态1.

我试过使用一个简单的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 
) 
+2

是的,foreach循环是正确的方法。看起来你没有尝试任何东西,否则你会显示代码(!) – MightyPork

+1

看到我的编辑包括我写的代码。我认为我的foreach工作不正常。 – user2656127

回答

1

我认为要正确遍历列表阵列?

$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; 
+0

啊,我没有重复列表 - 谢谢! – user2656127

+0

很高兴帮助,谢谢 – Sal00m

+0

另一个快速的问题 - 如果我正在试图弄清楚 - “每天有多少文章正在阅读” - 这会类似吗?有一个$ list [time_read]值,所以也许我可以解析这些值? – user2656127