erikvimz
erikvimz

Reputation: 5476

Count() returns different number of entries than var_dump()

Hey guys I'm tired and can't figure this one out so any help would be appreciated.

I have an array called $Row that I get from database table.

When I run var_dump($Row); I get the following:

array
  0 => string '1' (length=1)
  'id' => string '1' (length=1)
  1 => string 'erik' (length=4)
  'username' => string 'erik' (length=4)
  2 => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
  'password' => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
  3 => string '' (length=0)
  'email' => string '' (length=0)
  4 => string '0' (length=1)
  'date_join' => string '0' (length=1)
  5 => string '0' (length=1)
  'date_mod' => string '0' (length=1)
  6 => string '1' (length=1)
  'active' => string '1' (length=1)
  7 => string '1' (length=1)
  'admin' => string '1' (length=1)
  8 => string '0' (length=1)
  'deleted' => string '0' (length=1)

When I run echo count($Row); I get value 18.

Count and var_dump are right next to each other, there is no modification of $Row.

Question: Why does Count() return 18 entried when there are only 8 of them inside $Row shown by var_dump()? I guess I just don't understand count()... I checked http://php.net/manual/en/function.count.php, but still don't get it...

Edit: I understand whats wrong know, thanks everyone. Another question. How can I remove the ones that are in a string, for instance I want this kind of a table:

array
  0 => string '1' (length=1)
  1 => string 'erik' (length=4)
  2 => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
  3 => string '' (length=0)
  4 => string '0' (length=1)
  5 => string '0' (length=1)
  6 => string '1' (length=1)
  7 => string '1' (length=1)
  8 => string '0' (length=1)

* I'm using mysql_fetch_array() to retrieve the data and put it into a table.

Upvotes: 0

Views: 843

Answers (5)

lum
lum

Reputation: 1543

Actually, there are indeed 18 elements in your array:

  • indices 0 through 8 (9 elements in total)
  • id, username, ... another 9 elements

9 + 9 = 18

If you only want the numeric indices, and supposing you're using MySQL:

$Row = mysql_fetch_array($result, MYSQL_NUM)

Source: http://php.net/manual/en/function.mysql-fetch-array.php

Upvotes: 1

Shakti Singh
Shakti Singh

Reputation: 86416

You are having mixed array integer and associative and having 18 elements. This is I think returned from mysql_fetch_array which by default return associative and numeric indexed array.

Upvotes: 2

ab_dev86
ab_dev86

Reputation: 1982

I think that applying count to this:

0 => string '1' (length=1)
'id' => string '1' (length=1)

returns 2 instead of 1!

0 and id are two different items of the array! This taken for the others 8 elements make the count return 18

Upvotes: 1

smassey
smassey

Reputation: 5931

becuase you're getting back a mix of associative and numeric result set from you db ( you can precise which one you really want with FETCH_ASSOC or alike as a parameter to you dbvendor_query() function ).

There is very well 18 elements in your array.

Upvotes: 1

dan-lee
dan-lee

Reputation: 14502

Because your strings are also values in $Row they just seem to be indented because of the string char. That means

'id' => string '1' (length=1)
'username' => string 'erik' (length=4)
'password' => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
// ...

are also in your array

Upvotes: 1

Related Questions