Reputation: 101
Sorry i it is duplicated, but I don't seem to find the exact scenario I need to clarify.
So my question is why this:
var = array ();
echo count (var);
prints 0.
and this:
var = array (array());
echo count (var);
prints 1?
Thanks!
Upvotes: 2
Views: 63
Reputation: 2512
it is because there exist one value in the array and it does not matter whether the inner array is empty or not.
On index 0 an empty array exists which implies array is not blank so count results 1.
Upvotes: 1
Reputation: 932
In the first case, you create an empty array.
$var = array();
The contents of this array may look like this:
[ ]
There is nothing here. So, count($var)
is zero.
But if you create a nested
array, you would have
$var = array(array());
The contents of $var
would now be something like this:
[ [] ]
The inner array doesn't have anything inside it. But, the outer array has an empty array inside it. And therefore, its count
is 1
.
Consider an array to be a plastic box.
In the first case, you have nothing inside the box, and so the count
is 0
.
In the second case, however, you have an empty box inside the box. So, count
is 1
.
Upvotes: 1
Reputation: 9429
when you do this var = array (array());
you have two dimensional array. if you familiar java, it seems like
Object var[][] = {
{},
};
so, var.length
is ", but var[0].length
is 0
Upvotes: -1
Reputation: 360672
Because you've put an array into an array. Even if that inner array is empty, it's still SOMETHING.
It's like putting an empty plastic bag into another plastic bag. That outer bag now contains one item: another plastic bag.
Upvotes: 5