Miura-shi Miura-shi - 3 months ago 6
PHP Question

PHP array count off by 1

I have an array for each year being returned that holds all of the months and the months blog entries. I am trying to return the total count of blogs for the entire year. However, the number is one off every time. There should be a total of 6, but returns 5.

Array

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)


Loop

$year_count = 0;

foreach($year_data as $year_posts) {

$year_count += count($year_posts);
}

echo $year_count;


print_r of $year_posts

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
)

)

Answer

PHP has some robust array functions that are worth familiarizing yourself with. There's often an easier alternative to single-line loops of this sort.

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