sql - php - count elements in array -



sql - php - count elements in array -

i trying count elements in array, doens't work intended:

i have while loop, loops through user table:

while($refsdata=$refs->fetch()){ $new_array = array($refsdata['id']); print_r($new_array); $outcome = $rentedrefs->_paying($new_array); }

the print_r($new_array); gives me:

array ( [0] => 90427 ) array ( [0] => 90428 ) array ( [0] => 90429 ) array ( [0] => 90430 ) array ( [0] => 90431 ) array ( [0] => 90432 ) array ( [0] => 90433 ) array ( [0] => 90434 ) array ( [0] => 90435 ) array ( [0] => 90436 )

inside _paying function, count number of values array:

function _paying($referrals_array){ echo count($referrals_array); }

the problem is, above count($referrals_array); gives me: 1, when should 10

what doing wrong?

you create new array @ each step of loop. instead should written this:

$new_array = array(); while($refsdata=$refs->fetch()){ $new_array[] = $refsdata['id']; // print_r($new_array); } $outcome = $rentedrefs->_paying($new_array);

note moved _paying phone call outside loop, seems aggregating function. if not, you'd create process $refsdata['id'] instead - not whole array.

as sidenote, i'd recommend using fetchall() method (instead of fetch when need fill collection results of query. it'll trivial count number of resulting array.

php sql arrays pdo

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -