0

我的 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 ){抛出错误。这有什么问题?

4

1 回答 1

1

首先请检查 $employeeNames 这是数组还是不使用

  is_array($employeeNames)

然后执行foreach循环

于 2012-12-13T12:15:50.797 回答