遇到非数字值-Laravel

时间:2018-06-28 10:31:55

标签: php laravel

在如下所示的控制器中,我调用了generate中的函数model

函数运行时,抛出错误提示

  
    

遇到非数字值。

  

此错误似乎在函数$chromosomes[] = is_numeric($gene['code']);

中找到

所以我用is_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;
    }

1 个答案:

答案 0 :(得分:2)

您要更改

$chromosomes[] = is_numeric($gene['code']);

$chromosomes[] = (int)($gene['code']);

或者也许

$chromosomes[] = (float)($gene['code']);