Php 遇到非数值-Laravel
在下面的控制器中,我调用函数Php 遇到非数值-Laravel,php,laravel,Php,Laravel,在下面的控制器中,我调用函数generate,该函数位于我的模型中 当函数运行时,它抛出一个错误 遇到非数值 此错误似乎出现在函数$chromoses[]=is_numeric($gene['code'])中 所以我使用了is_numeric来转换为numeric 为什么这种情况还在发生?多谢各位 控制器 $timeTable = $timeTable->generate($condition['class']); 型号 public function generate($class)
generate
,该函数位于我的模型中
当函数运行时,它抛出一个错误
遇到非数值
此错误似乎出现在函数$chromoses[]=is_numeric($gene['code'])中代码>
所以我使用了is_numeric
来转换为numeric
为什么这种情况还在发生?多谢各位
控制器
$timeTable = $timeTable->generate($condition['class']);
型号
public function generate($class)
{
$this->is_general = $class;
if (!$this->slotsAreEnough()) {
abort(500, 'The number of units exceed the available spaces.');
}
$this->makeChromosomes();
$this->createInitialGeneration();
$this->startSelection();
return $this->timeTable;
}
private function makeChromosomes()
{
$chromosomes = array();
foreach ($this->gene as $gene) {
for ($x = 0; $x < $gene['units']; $x++) {
$chromosomes[] = is_numeric($gene['code']);
}
}
$this->chromosomes = $chromosomes;
return $this;
}
private function startSelection()
{
$totalSlot = $this->daysOfWeek * $this->sizeOfDay;
$size = count($this->chromosomes);
for ($x = 0; $x < $size; $x++) {
$seed = rand(1, $totalSlot);
for ($y = 0; $y < $this->daysOfWeek; $y++) {
for ($z = 0; $z < $this->sizeOfDay; $z++) {
if ($this->hasAssigned($seed)) continue;
if ($this->isBreakTimeZone($seed)) continue;
$row = (int) ($seed / $this->sizeOfDay);
$col = $seed % $this->sizeOfDay;
if ($this->hasLevelWideCourses() && $this->levelWideCourses[$row][$col] != '-') {
$this->timeTable[$row][$col] = $this->levelWideCourses[$row][$col];
} else {
$this->timeTable[$row][$col] = $this->chromosomes[$x];
}
if (isset($this->chromosomes[$x + 1])){
if ($this->chromosomes[$x + 1] === $this->chromosomes[$x] && !$this->hasUsedDoublePeriods($this->chromosomes[$x])) {
$this->timeTable[$row][$col + 1] = $this->chromosomes[$x];
$this->hasDoublePeriod[] = $this->chromosomes[$x];
}
}
$this->usedSlots[] = $seed;
$this->selectVenue($this->chromosomes[$x], $row, $col);
}
}
}
return $this;
}
公共函数生成($class)
{
$this->is_general=$class;
如果(!$this->slotsAreEnough()){
中止(500,“单位数量超过可用空间”);
}
$this->make染色体();
$this->createInitialGeneration();
$this->startSelection();
返回$this->时间表;
}
私有函数
{
$chromes=array();
foreach($this->gene as$gene){
对于($x=0;$x<$gene['units'];$x++){
$chromoses[]=是数字($gene['code']);
}
}
$this->chromoses=$chromoses;
退还$this;
}
私有函数startSelection()
{
$totalSlot=$this->daysOfWeek*$this->sizeOfDay;
$size=count($this->chromes);
对于($x=0;$x<$size;$x++){
$seed=rand(1$totalSlot);
对于($y=0;$y<$this->daysOfWeek;$y++){
对于($z=0;$z<$this->sizeOfDay;$z++){
如果($this->hassassigned($seed))继续;
如果($this->isBreakTimeZone($seed))继续;
$row=(int)($seed/$this->sizeOfDay);
$col=$seed%$this->sizeOfDay;
如果($this->hasLevelWideCourses()&&&$this->levelWideCourses[$row][$col]!='-')){
$this->chedule[$row][$col]=$this->levelWideCourses[$row][$col];
}否则{
$this->时间表[$row][$col]=$this->染色体[$x];
}
if(isset($this->chromes[$x+1])){
如果($this->染色体[$x+1]===$this->染色体[$x]&&!$this->hasueddoubleperiods($this->染色体[$x])){
$this->时间表[$row][$col+1]=$this->染色体[$x];
$this->hasdubleperiod[]=$this->染色体[$x];
}
}
$this->usedSlots[]=$seed;
$this->selectVenue($this->chromes[$x],$row,$col);
}
}
}
退还$this;
}
要更改吗
$chromoses[]=是数字($gene['code'])代码>
到
$chromoses[]=(int)($gene['code'])代码>
或许
$chromoses[]=(float)($gene['code'])代码>要更改吗
$chromoses[]=是数字($gene['code'])代码>
到
$chromoses[]=(int)($gene['code'])代码>
或许
$chromoses[]=(float)($gene['code'])
是数值的
不转换为数值,如果它是数值的,它将返回一个布尔值。@apokryfos,有任何函数可以帮助我转换为数值吗?是数值的
不转换为数值的,如果它是数值的,它将返回一个布尔值。@apokryfos,有什么函数可以帮我转换成数值吗?float
可能更好,因为整数包含在float中,但不是其他方法。你可以使用$chromes[]=intval($gene['code'])代码>它将字符串中的数字转换为整数格式。因此,当您使用is_numeric时,字符串中的数字将返回true,我的意思是在双引号或单引号中。float
可能更好,因为整数包含在float中,而不是其他方法。您可以使用$chromes[]=intval($gene['code'])代码>它将字符串中的数字转换为整数格式。所以当使用is_numeric时,字符串中的数字将返回true,我指的是双引号或单引号。