1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

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,333
    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