【问题标题】:A non-numeric value is encountered - Laravel遇到非数字值 - Laravel
【发布时间】:2018-06-28 10:31:54
【问题描述】:

在我的控制器中,我调用了函数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;
    }

【问题讨论】:

  • is_numeric 不会转换为数字,如果是数字则返回布尔值。
  • @apokryfos,有什么函数可以帮我转换成数字吗?

标签: php laravel


【解决方案1】:

你想改变

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

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

或许

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

【讨论】:

  • float 可能更好,因为整数包含在浮点数中,但反之则不然
  • 您可以使用$chromosomes[] = intval($gene['code']); 它将字符串中的数字转换为整数格式。因此,当您使用 is_numeric 时,它将为字符串中的数字返回 true,我的意思是双引号或单引号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-28
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 2018-02-18
相关资源
最近更新 更多