数组未正确填充

时间:2010-07-05 17:03:13

标签: php arrays count loops

我使用以下代码填充数组:

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

postcodeUnknown使用表前缀和名为postcodes的数组中的元素,从mysql查询返回第一行。 $ postcodes包含每次循环时应返回不同行的字符串。

我希望创建一个类似于:

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

但事实并非如此。相反,它会创建一个包含第一个结果的奇怪数组,直到满足条件,例如:

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
     )
)

我不知道为什么。任何人都比我更清楚。

3 个答案:

答案 0 :(得分:0)

呃,在postcodeUnknown上添加一个打印件并检查它是否实际上一直传递不同的邮政编码或同样的东西?

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

另外,为什么有两个不同的变量? ($ quantitys和$ postcodes)。

此外,您可能希望用for:

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

答案 1 :(得分:0)

你的变量数不应该是一个字符串,试试这个

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

答案 2 :(得分:0)

尝试

$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>";

尝试查看其中任何一个是否获得您正在寻找的输出。