$a = 1;
$b = 1;
while($a <= 4) {
$main_marks = array();
while($b <= 4) {
$main_marks[] = 50;
$b++;
}
print_r($main_marks);
$a++;
}
我从该代码得到的输出是:
Array (
[0] => 50
[1] => 50
[2] => 50
[3] => 50
)
Array ()
Array ()
Array ()
但我希望所有数组变量都带有值。 请帮帮我。
答案 0 :(得分:1)
在循环外创建变量。
$a = 1;
$b = 1;
$main_marks = array();
while($a <= 4) {
while($b <= 4) {
$main_marks[] = 50;
$b++;
}
print_r($main_marks);
$a++;
}
答案 1 :(得分:0)
$a=0;
$b=0;
while($a < 4) {
$b = 0;
while($b < 4) {
$main_marks[$a][$b] = 50;
$b++;
}
$a++;
}
print_r($main_marks);
您希望为预期输出填充多维数组。
答案 2 :(得分:0)
<?
$main_marks = array();
for($a=0;$a<4;$a++)
{
for($b=0;$b<4;$b++)
{
$main_marks[$a][$b]=50;
}
}
print_r($main_marks);
?>
Output:
Array
([0] => Array ( [0] => 50 [1] => 50 [2] => 50 [3] => 50 )
[1] => Array ( [0] => 50 [1] => 50 [2] => 50 [3] => 50 )
[2] => Array ( [0] => 50 [1] => 50 [2] => 50 [3] => 50 )
[3] => Array ( [0] => 50 [1] => 50 [2] => 50 [3] => 50 ) )
答案 3 :(得分:0)
将$b=1;
放在外部循环中:
$a=1;
while($a <= 4) {
$b=1;
$main_marks = array();
while($b <= 4) {
$main_marks[] = 50;
$b++;
}
print_r($main_marks);
$a++;
}
现在您为每个数组重置$b
计数器