Anders Hedeager
Anders Hedeager

Reputation: 117

array length count doesn't match

When i set a global array as this

 $items[$users[$clientID]['room']] = array("seat" => $seat, "item_id" => $q[1], "room" => $users[$clientID]['room']);

it is

  $items[4] = array("seat" => 20, "item_id" => 10, "room" => 4);

but when i do a count it telling the length of $items[4] is 3? It should only count 1 because i only have "1" item.

for ( $i=0;$i<count($items[$users[$clientID]['room']]);$i++):
    //something 
endfor;

print_r($items[$users[$clientID]['room']]);

outputs:

Array
( 
    [seat] => 43
    [item_id] => 46
    [room] => 5 
)
 COUNT 3

What am i doing wrong?

Upvotes: 0

Views: 259

Answers (2)

Ravi
Ravi

Reputation: 2086

$items[$users[$clientID]['room']] is an array and count() returns no of element in an array, if the value to count is not an array it will return 1. See this link for more information about count function.

Count Function - PHP

Upvotes: 1

Mark Baker
Mark Baker

Reputation: 212402

You have defined $items[$users[$clientID]['room']] as one array: but an array that comprises three items - seat, item_id and room - and it is those individual array items that you are counting.

Upvotes: 2

Related Questions