PHP数组减1

时间:2015-05-28 17:23:15

标签: php arrays foreach

我每年都会有一个数组,其中包含所有月份和月份的博客条目。我试图返回全年的博客总数。但是,这个数字每次都是一次。应该总共有6个,但是返回5.

阵列

array (size=2)
  '04' => 
    array (size=1)
      0 => 
        array (size=7)
          'entry_id' => string '5' (length=1)
          'title' => string 'Post Title' (length=36)
          'year' => string '2015' (length=4)
          'month' => string '04' (length=2)
          'day' => string '30' (length=2)
          'date' => string 'Apr. 30, 2015' (length=13)
          'link' => string 'www.brand.com/blog/title' (length=86)
  '05' => 
    array (size=5)
      0 => 
        array (size=7)
          'entry_id' => string '4' (length=1)
          'title' => string 'Post Title' (length=56)
          'year' => string '2015' (length=4)
          'month' => string '05' (length=2)
          'day' => string '14' (length=2)
          'date' => string 'May. 14, 2015' (length=13)
          'link' => string 'www.brand.com/blog/title' (length=96)
      1 => 
        array (size=7)
          'entry_id' => string '7' (length=1)
          'title' => string 'Post Title' (length=58)
          'year' => string '2015' (length=4)
          'month' => string '05' (length=2)
          'day' => string '15' (length=2)
          'date' => string 'May. 15, 2015' (length=13)
          'link' => string 'www.brand.com/blog/title' (length=105)
      2 => 
        array (size=7)
          'entry_id' => string '8' (length=1)
          'title' => string 'Post Title' (length=45)
          'year' => string '2015' (length=4)
          'month' => string '05' (length=2)
          'day' => string '15' (length=2)
          'date' => string 'May. 15, 2015' (length=13)
          'link' => string 'www.brand.com/blog/title' (length=95)
      3 => 
        array (size=7)
          'entry_id' => string '9' (length=1)
          'title' => string 'Post Title' (length=52)
          'year' => string '2015' (length=4)
          'month' => string '05' (length=2)
          'day' => string '15' (length=2)
          'date' => string 'May. 15, 2015' (length=13)
          'link' => string 'www.brand.com/blog/title' (length=98)
      4 => 
        array (size=7)
          'entry_id' => string '10' (length=2)
          'title' => string 'Post Title' (length=33)
          'year' => string '2015' (length=4)
          'month' => string '05' (length=2)
          'day' => string '15' (length=2)
          'date' => string 'May. 15, 2015' (length=13)
          'link' => string 'www.brand.com/blog/title' (length=83)

循环

    $year_count  = 0;

    foreach($year_data as $year_posts) {

       $year_count += count($year_posts);
    }

    echo $year_count;

$ year_posts的print_r

     Array
    (
        [0] => Array
            (
                [entry_id] => 6
                [title] => Blog Title 
                [year] => 2014
                [month] => 05
                [day] => 14
                [date] => May. 14, 2014
                [link] => blog link
            )

    )


   Array
    (
        [0] => Array
            (
                [entry_id] => 5
                [title] => Blog Title
                [year] => 2015
                [month] => 04
                [day] => 30
                [date] => Apr. 30, 2015
                [link] => blog-url
            )

    )


    Array
    (
        [0] => Array
            (
                [entry_id] => 4
                [title] => Blog Title
                [year] => 2015
                [month] => 05
                [day] => 14
                [date] => May. 14, 2015
                [link] => blog-url
            )

        [1] => Array
            (
                [entry_id] => 7
                [title] => Blog Title
                [year] => 2015
                [month] => 05
                [day] => 15
                [date] => May. 15, 2015
                [link] => blog-url
            )

        [2] => Array
            (
                [entry_id] => 8
                [title] => Blog Title
                [year] => 2015
                [month] => 05
                [day] => 15
                [date] => May. 15, 2015
                [link] => blog-url
            )

        [3] => Array
            (
                [entry_id] => 9
                [title] => Blog Title
                [year] => 2015
                [month] => 05
                [day] => 15
                [date] => May. 15, 2015
                [link] => blog-url
            )

        [4] => Array
            (
                [entry_id] => 10
                [title] => Blog Title
                [year] => 2015
                [month] => 05
                [day] => 15
                [date] => May. 15, 2015
                [link] => blog-url
            )

    )

1 个答案:

答案 0 :(得分:4)

PHP有一些值得熟悉的强大array functions。对于这种单行循环,通常有一种更容易的替代方法。

$post_count = array_sum(array_map('count', $year_data));