0
我很難完成一項簡單的任務。我有一個方法會產生隨機數,並根據結果爲數組變量賦予特定的結果。我想要做的是通過實例方法獲取該數組變量,該方法將從另一個類中調用。php全局變量和實例變量利用率
<?php
class MyClass
{
public $results = array(array());
public function simulated_games()
{
$game_series1=array(23,34,56);
$game_series2=array(31,42,67);
$iter_wins=array(array());
for($i=0; $i<10;$i++)
{
$random = rand(0,100);
for($b=0; $b<1;$b++)
{
if($random <= $game_series1[0])
{
$iter_wins[$i][$b]=3;
}
else if($random <= $game_series2[0]+$game_series2[1])
{
$iter_wins[$i][$b]=1;
}
}
}
$results=$iter_wins;
}
>here i create method just to return variable
public function get_simulated_games()
{
return $this->results;
}
}
<?php
$a= new MyClass();
$a->simulated_games();
$array = array();
>here is the issue, it return just 1, but supposed to return range numbers
$array=$a->get_simulated_games();
for($f=0; $f<sizeof($array);$f++)
{
for($g=0; $g<5;$g++)
{
echo $array[$f][$g];
}
echo '<br>';
}
?>
'simulated_games()'正在修改局部變量,而不是實例一個 –
''random','$ game_series1'和'$ game_series2'沒有在'simulation_games'方法中定義。 –
'simulated_games()'似乎沒有做任何事情,除非生成未定義的變量警告。 – jeroen