Array

Discussion in 'PHP' started by dean5000v, May 18, 2010.

  1. #1
    lets say i have an array like:

    Array ( [0] => [1] => 2 [2] => 3 [3] => 3 [4] => 3 )

    now i want to count the total number of items in the array so i use:

    count($items)

    however that counts the [0] key which has no value, is there a way to only count fields with a value.
     
    dean5000v, May 18, 2010 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    Well, an empty value is still a value. If you want to check for empty values, you probably will need to loop through the array to check each value like so:

    $count = 0;
    foreach ($array as $val) {
    	if ($val) $count++;
    }
    PHP:
     
    digitalpoint, May 18, 2010 IP