【发布时间】: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,有什么函数可以帮我转换成数字吗?