Php kohana框架中的in_数组函数中的数据类型错误?

Php kohana框架中的in_数组函数中的数据类型错误?,php,kohana,Php,Kohana,我有两个控制器,一个是基本控制器,另一个是基本控制器中的主题,我从模型中定义了一些数组,看: class Controller_Base extends Controller_Template { public $template = 'main'; public function before() { parent::before(); $webs = array(); $apps = array();

我有两个控制器,一个是基本控制器,另一个是基本控制器中的主题,我从模型中定义了一些数组,看:

class Controller_Base extends Controller_Template {

    public $template = 'main';

    public function before()
    {
        parent::before();

        $webs = array();
        $apps = array();

        $app = new Model_Application();
        $apps = $app->get_all();

        $web = new Model_Web();
        $webs = $web->get_all();

        $this->template->content = '';
        $this->template->styles = array('style');
        $this->template->scripts = '';

        $this->template->webs = $webs;
        $this->template->apps = $apps;

    }

}
在控制器主题中,我使用了_数组中的函数

class Controller_Subject extends Controller_Base {

public function action_all()
{

    $url = $this->request->param('url');

    $this->template->caption = $url;


    if (in_array($url,$this->template->webs)) { 
        echo "web";
    }
        elseif (in_array($url,$this->template->apps)) { 
        echo "apps";
    }

    $links = array("a"=>"1","b"=>"2");

    $view = View::factory('subject')
                ->set('links',$links);

    $this->template->content = $view;

}

}
但是Kohana给了我一个错误:

ErrorException [ Warning ]: in_array() [<a href='function.in-array'>function.in-array</a>]: Wrong datatype for second argument

怎么了?

您需要数组变量而不是数据库结果对象:

...
$apps = $app->get_all()->as_array();
...
$webs = $web->get_all()->as_array();
...

您需要数组变量而不是数据库结果对象:

...
$apps = $app->get_all()->as_array();
...
$webs = $web->get_all()->as_array();
...

模型返回的数据,即数据库\u MySQL\u结果,需要先更改为数组。 你可以用

foreach($query as $v) 
    $arr[]=$v; 
return $arr;

在get_all.

中,需要首先将模型返回的数据(即数据库\u MySQL\u结果)更改为数组。 你可以用

foreach($query as $v) 
    $arr[]=$v; 
return $arr;
在get_all.

中,您可以执行var_dump$webs并查看该变量中的数据。我猜有一个对象,而不是数组。是的,有一个对象:objectDatabase\u MySQL\u Result24 7{[\u internal\u row:protected]=>int0[\u query:protected]=>string18 SELECT*FROM webs[\u result:protected]=>resource73类型MySQL结果[\u total\u rows:protected]=>int5[\u current\u row:protected]=>int0[\u as\u对象:protected]=>boolfalse[\u object\u params:protected]=>NULL}您可以执行var\u dump$webs并查看该变量中的数据。我猜是有一个对象,而不是数组。是的,有一个对象:objectDatabase\u MySQL\u Result24 7{[\u internal\u row:protected]=>int0[\u query:protected]=>string18 SELECT*FROM webs[\u result:protected]=>resource73类型MySQL result[\u总计\u行:受保护]=>int5[\u当前\u行:受保护]=>int0[\u作为\u对象:受保护]=>boolfalse[\u对象参数:受保护]=>NULL}