0

I am trying to count elements in an array, but it doens't work as intended:

I have a while loop, which loops through my 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 the _paying function, I count the number of values from the array:

function _paying($referrals_array){

echo count($referrals_array);


}

The problem is, that the above count($referrals_array); just gives me: 1, when it should be 10

What am I doing wrong?

2
  • On each iteration of your while loop you're overwriting $new_array. Commented Nov 2, 2014 at 12:24
  • Your array is only returning one element. Look at your output. Each array is Array[0]. Commented Nov 2, 2014 at 12:24

4 Answers 4

2

You create a new array at each step of the loop. Instead it should be written like this:

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

$outcome = $rentedrefs->_paying($new_array);

Note that I moved the _paying call outside the loop, as it seems to be the aggregating function. If not, you'd most probably make it process $refsData['id'] instead - not the whole array.

As a sidenote, I'd strongly recommend using fetchAll() method (instead of fetch when you need to fill a collection with results of a query. It'll be trivial to count the number of the resulting array.

Sign up to request clarification or add additional context in comments.

2 Comments

The while loop I showed in the question, is actually a nested loop. Meaning there is another while loop outside that loop. Will that be a problem, if I am placing the $outcome outside of that nested loop, but in the first loop?
You didn't show the outer loop, but the way I see it, the code I've shown is a direct replacement for what you have at the moment.
1

You are creating $new_array as a new array with the single element $refsData['id']. The count of 1 is therefore correct.

To get the number of results, either use a COUNT(*) select to ask your sql server, or add a counter to your loop, like this:

$entries = 0;
while($refsData=$refs->fetch()){
    $new_array = array($refsData['id']);
    print_r($new_array);
    $entries++;

    $outcome = $rentedrefs->_paying($new_array);
}

echo $entries;

4 Comments

This gives me "100" when I echo $entries
Then you have 100 results, I guess.
there is only 10 rows in the table.
Since i don't know your query, I can only guess. You could be generating a much larger number of results by joining the table with itself.
0

It works properly, in each circulation of loop you have one array, so in first you have:

Array
(
    [0] => 90427
)

in 2nd:

 Array
(
    [0] => 90428
)

and so on.

It should work properly:

var $count = 0;
while($refsData=$refs->fetch()){
    $new_array = array($refsData['id']);
        $count += count($new_array);    
        $outcome = $rentedrefs->_paying($new_array);
    }

Comments

0

You are not adding elements to an array, but creating a new array each iteration. To add elements, just do:

$new_array[] = $refsData['id'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.