开发者

Array not populating correctly

I am using the following code to populate an array:

$number = count ($quantitys);
    $count = "0";
    while ($count < $number) {
        $ref[$count] =  postcodeUnknown ($prefix, $postco开发者_开发知识库des[$count]);  
        $count = $count +1;
    }

postcodeUnknown returns the first row from a mysql query, using a table prefix and an element from an array named postcodes. $postcodes contains strings that should return a different row each time though the loop.

Which I'd expect to create an array similar to:

Array ([0] => 
       Array ([0] => some data [1] => more data) 
[1] => 
       Array ([0] => second row [1] => even more...)
)

But it's not. Instead it's creating a strange array containing the first results over and over until the condition is met, eg:

simplified result of print_r($ref);

Array (
   [0] => Array ([0] => some data [1] => more data)
) 
Array(
   [0] => Array (
        [0] => the first arrays content... 
        [1] => ...repeated over again until the loop ends
     )
)

And I'm at a loss to understand why. Anyone know better than me.


Try

$number = count ($quantitys);
$count = "0";
while ($count < $number) {
    $ref1[$count] =  postcodeUnknown ($prefix, $postcodes[$count]);
    $ref2[] =  postcodeUnknown ($prefix, $postcodes[$count]);
    $ref3[$count][] =  postcodeUnknown ($prefix, $postcodes[$count]);
    $count = $count +1;
}


echo "<pre>";
print_r($ref1);
print_r($ref2);
print_r($ref3);
echo "</pre>";

Try that to see if any of those get an output you are looking for.


Uh, add a print to postcodeUnknown and check if its actually passing different postcodes or the same thing all the time?

ie

function postcodeUnkown($a,$b){
    echo var_dump($a).var_dump($b);
    //the rest
}

Also, why two different variables? ($quantitys and $postcodes).

Also, you might want to replace the while loop with for:

for($i=0;$i<$number;$i++){
    $ref[$i] =  postcodeUnknown ($prefix, $postcodes[$i]);        
}


your variable count should not be a string, try this

$number = count ($quantitys);
    $count = 0;
    while ($count < $number) {
        $ref[$count] =  postcodeUnknown ($prefix, $postcodes[$count]);  
        $count = $count +1;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜