2

Is there a way to find out how many value-elements are in one array in all nesting levels?

I want to find out how many are in $_REQUEST to give out a warning if the limit dictated by the max_input_vars directive is nearly reached?

I want to count only the real values, so array-elements should not be counted if they are another array (see http://pastebin.com/QAKxxqJf)

rubo77
  • 19,527
  • 31
  • 134
  • 226
  • 3
    http://php.net/manual/en/function.count.php - look at the options for `$mode` – Mark Sep 30 '13 at 12:42
  • @rubo77 I recently had the same problem. See my [answer](http://stackoverflow.com/a/19096811/1873446) to know how I solve it. – letiagoalves Sep 30 '13 at 14:30

5 Answers5

6

use: count($array_name, COUNT_RECURSIVE);

as count method takes a second argument which is the mode. int COUNT_NORMAL or COUNT_RECURSIVE

mamdouh alramadan
  • 8,349
  • 6
  • 36
  • 53
  • 1
    I think this is not completely correct, because a recursive count also counts keys into the total, see: http://pastebin.com/QAKxxqJf – rubo77 Sep 30 '13 at 14:16
  • Substract count from count recursive you will get the magic going. In Advanced cases you will need to do some if and foreach tricks :-) – mamdouh alramadan Sep 30 '13 at 20:10
  • 1
    This is not the correct answer! `max_input_vars` doesen't count the subarray containers while `count($a, 1)` does (although `count($a, COUNT_RECURSIVE);` is a good thing in general ;) ) – rubo77 Oct 01 '13 at 00:37
2

Use array_walk_recursive to count all elements that are not of type array or object:

$count = 0;
array_walk_recursive($a, function($v) use(&$count) {
    if(!is_object($v)) ++$count;      //or if(is_string($v))
});
letiagoalves
  • 11,224
  • 4
  • 40
  • 66
  • Why do you need `if(!is_array($v) && !is_object($v))`? with just plain `++$count;` I get the same result for `$count` (tested with the multidimensional array `$_REQUEST`) – rubo77 Sep 30 '13 at 14:49
  • so it ignores object values. If you had for example `3 => new foo()` in your array it will count that as an element. You could just check if it is a `string` if you want. It depends on the case. However I think you do not need `!is_array($v)`. Please confirm and I will remove that validation from my answer. – letiagoalves Sep 30 '13 at 14:56
  • i tested it, you only need `if(!is_object($v))` or all objects would be counted as one element each, not though the variables inside the object, they are ignored all the time. – rubo77 Sep 30 '13 at 15:10
  • @rubo77 thanks. Seeing your pastebin link I would check for `string` type instead. However it is best to mantain `object` validation in my answer for others users in the future since it is more generic – letiagoalves Sep 30 '13 at 15:17
  • [When was function use closures implemented in PHP?](http://stackoverflow.com/questions/20411631/when-was-function-use-closures-implemented-in-php) – rubo77 Dec 05 '13 at 21:58
  • closures are implemented in PHP 5.3 so your solution is effective, but since this is not possible in PHP 5.2 I have to use muslim-idris' solution – rubo77 Dec 05 '13 at 22:29
  • you did not specified PHP version in your question so last stable version was taken in account when answering this question – letiagoalves Dec 06 '13 at 10:45
  • sure, but I read somewhere, I have to enter the question that solve MY problem. I have to administer different php versions starting from 5.2 – rubo77 Dec 06 '13 at 15:14
1

This is my solution for PHP <= 5.2

/**
 * Counts only the real values, so array-elements
 * are not counted if they are another array.
 *
 * @param array $array
 * @return int
 * @author Muslim Idris
 */
function count_recursive($array) {
    $count = 0;
    foreach ($array as $v) {
            if (is_array($v)) $count += count_recursive($v);
            else $count++;
    }
    return $count;
}
0

I cannot use count($_REQUEST, COUNT_RECURSIVE), so I used:

function count_recursive($array) {
  $count = 0;
  foreach ($array as $id => $_array) {
    if (is_array ($_array)) $count += count_recursive ($_array);
    else $count += 1;
  }
  return $count;
} 
$num=count_recursive($_REQUEST);
$max=ini_get('max_input_vars');
$debug=true;
if($debug and $max - $num < 50) {
  echo "Warning:Number of requests ($num) is near the value of max_input_vars:$max";
}

(although there is still a strange thing is on my testing server, but thats another question: why is the number of elements in $_REQUEST smaller than the limit i set by max_input_vars? and maybe a bug in PHP)

Community
  • 1
  • 1
rubo77
  • 19,527
  • 31
  • 134
  • 226
-2

check following link for all option of using count function

http://php.net/manual/en/function.count.php

Sunil Verma
  • 2,490
  • 1
  • 14
  • 15