0

i need to count a multidimensional array, but with some rules.

My array is this:

<?$array[$iddom][$idrisp][$idopt][$iddoc][$idut]=$text;?>

and i have for example this values:

[1][1][0][0][31248]=
[1][1][0][0][46619]=
[2][4][0][0][31248]=
[2][5][0][0][46619]=
[3][6][1][0][31248]=
[3][6][3][0][46619]=
[3][7][3][0][31248]=
[3][7][2][0][46619]=
[3][8][1][0][31248]=
[3][8][1][0][46619]=
[4][9][0][0][46619]=
[4][11][0][0][31248]=
[5][13][5][0][31248]=
[5][13][6][0][46619]=
[5][14][4][0][31248]=
[5][14][4][0][46619]=
[5][15][6][0][31248]=
[5][15][5][0][46619]=
[6][0][7][6][31248]=
[6][0][7][6][46619]=

if i want to count only a part of array, i use this code:

<?$count=array_sum(array_map("count", $array[$key]));?>

in this case count is correct

[2][4][0][0][31248]=
[2][5][0][0][46619]=

the result of count is 2

but in this case the result is 1

[1][1][0][0][31248]=
[1][1][0][0][46619]=

how i can count dinamically arrays multidimensional? because in anothe case i need to count for example another level of array, like this:

<?$count=array_sum(array_map("count", $array[$key][$key2]));?>

thanks

1 Answer 1

1

You could wrap these functions:

$count = count(iterator_to_array(new RecursiveIteratorIterator(
                 new RecursiveArrayIterator($array)
         )));

Replace $array with whichever key you want to apply the count to, like $array[$key][$key2].

Sign up to request clarification or add additional context in comments.

1 Comment

finally i find the sign :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.