我的foreach中有一个问题是“为foreach提供了无效的参数......”
这是我正在处理的代码:
protected function jsGenerateResourcesAutocomplete(){
$employeeNames = $this->employeeNames;
$html = "";
$html .= '<script>' . PHP_EOL;
$html .= 'var employeeNames = [' . PHP_EOL;
foreach( $employeeNames as $employeeName ){
$html .= '"' . $employeeName->getEmployeeName() . '",' . PHP_EOL;
}
$html .= '];' . PHP_EOL;
$html .= '$(function() {' . PHP_EOL;
$html .= '$(#resource-input input:text").each(function() {' . PHP_EOL;
$html .= '$(this).autocomplete({source: employeeNames});' . PHP_EOL;
$html .= '})' . PHP_EOL;
$html .= '})' . PHP_EOL;
$html .= '</script>' . PHP_EOL;
return $html;
}
在这一行中: foreach($ employeeNames as $ employeeName){会抛出错误。这有什么问题?
答案 0 :(得分:1)
首先请使用
检查$ employeeNames这是否为数组 is_array($employeeNames)
然后执行foreach循环