2014-07-15 29 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