Jump to content

Archived

This topic is now archived and is closed to further replies.

Synergic

why does the sizeof and count give incorrect array size?

Recommended Posts

why do both these functions:


sizeof -- Alias of count()
count -- Count elements in an array, or properties in an object

give an array size of 1 even though there's nothing inside it?
I then add something to it and it gives me 1 still then 2. what is in index 0? i need to do some manipulation and have to check if an array is empty.

Share this post


Link to post
Share on other sites
Gives me 0 if empty, it dosent give me 1.
And if it is a variable then ofcource it gives 1.

Share this post


Link to post
Share on other sites
yeah but if i add something to the array it gives me 1. It's hard to tell if its really empty or not...

Share this post


Link to post
Share on other sites
The answer is in the manual - http://ca.php.net/manual/en/function.count.php

[quote]count() may return 0 for a variable that isn't set, but it may also return 0 for a variable that has been initialized with an empty array. Use isset() to test if a variable is set.[/quote]

Share this post


Link to post
Share on other sites
[code]
<? if (isset($_SESSION['cart']))
{
echo 'Array is set.';
} ?>

<?echo '<pre>';
  print_r( $_SESSION );
echo '</pre>';?>
[/code]

result:

[code]
Array is set

Array
(
    [cart] => ShoppingCartClass Object
        (
            [items] => Array
                (
                )

            [totalPrice] =>
        )

)
[/code]

what gives?  :-\

Share this post


Link to post
Share on other sites
ok fixed, the problem was 'cart' was an object i got confused with 'it' being an array. I accessed the array inside [i] cart [/i] which is [i] items [/i] and recieve a 0 :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.