user3142616
user3142616

Reputation: 13

Count array keys

Array
(
[0] => Array
    (
        [name] => WWW
    )

[1] => Array
    (
        [name] => Hi
    )

[2] => Array
    (
        [name] => Hello
    )

[3] => Array
    (
        [name] => World
    )

)

I have the above array and I want to count the number of keys.

When using the following code

$temp = array_keys($array);
echo $temp;

the result is 2 instead of 4 (0,1,2,3). What I'm doing wrong ?

Upvotes: 1

Views: 204

Answers (3)

Rafael Soufraz
Rafael Soufraz

Reputation: 964

Very simple buddy. Look this:

 $array = array(0 => 100, "color" => "red");
 print_r(count($array));

php.net help you for all! ;)

Upvotes: -1

Joseph Silber
Joseph Silber

Reputation: 219938

Just count the array itself: count($array).

There's always the same amount of keys as there are values!

Upvotes: 4

Teknotica
Teknotica

Reputation: 1136

You need to count the array in order to get a number:

  $arr = array
    (
        "0" => array
        (
            "name" =>"WWW"
        ),

    "1" => array
    (
        "name" => "Hi"
    ),

    "2" => array
    (
        "name" => "Hello"
    ),

    "3" => array
    (
        "name" => "World"
    )

    );

    $keys_count = count($arr);
    echo $keys_count;

Upvotes: 4

Related Questions