views:

46

answers:

3

Hi

I am using the following code to populate an array:

$number = count ($quantitys);
    $count = "0";
    while ($count < $number) {
        $ref[$count] =  postcodeUnknown ($prefix, $postcodes[$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.

A: 

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]);        
}
Robus
This has not fixed the malformed array problem. As far as I can tell the root of the issue is something to do with the $count iteration. While postcodeUnknown is being called the appropriate number of times, it seems to call with $postcodes[0] regardless of how many times the loop has run. I am using MAMP by the way for development.
YsoL8
A: 

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;
    }
cdnicoll
This won't be the problem as PHP will still increment a string, by turning it into an int
Lizard
checked this to be sure - no change.
YsoL8
A: 

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.

Lizard
None of these have worked. I'm going to try gutting the unknownPostcode function and check it for faults.
YsoL8
Fixed. I tried your suggestion - no luck. However I then tried changing count ($quantitys) to count ($postcodes). Which shouldn't of effected anything as they are always arrays of the same length, It worked anyway.
YsoL8
Ok, if unknownPostcode returns an array then $ref[$count] = unknownPostcode() should work.
Lizard
Cool accept away!
Lizard