$tmp = 0; foreach($result['response']['items'] as $post ) { if ($post['date'] <= $only_old_post_time ) { $tmp++; } } if (!$tmp){ $offset += 100; } 

How to write this piece of code more elegantly, correctly, so that it works faster, and the mb can be done like standard functions?

  • can be $tmp = count(array_filter(... - splash58

1 answer 1

You have a completely working version: you have to iterate over the array anyway. It is possible as @ splash58 wrote (although an extra array is created there), or via array_reduce() :

 $tmp = array_reduce( $result['response']['items'], "is_old", 0); function is_old( $count, $post) { return $post['date'] <= $only_old_post_time ? ++$count : $count; }