简体   繁体   English

kohana框架中的in_array函数中的数据类型错误?

[英]Wrong data type in in_array function in kohana framework?

I have two controllers, one is base controller, other is subject in the base controller I defined some arrays from Model, look: 我有两个控制器,一个是基本控制器,另一个是基本控制器中的subject ,我从Model定义了一些数组,请看:

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;

    }

}

in the controller subject I am using function in_array 在控制器主题中,我正在使用函数in_array

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;

}

}

but the Kohana returns me an error: 但是Kohana给我返回了一个错误:

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

Whats wrong? 怎么了?

You need array variables instead of Database_Result objects: 您需要数组变量而不是Database_Result对象:

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

The Models returned data, that is, Database_MySQL_Result , needs to be changed to an array first. 模型返回的数据,即Database_MySQL_Result ,需要首先更改为数组。 you can use 您可以使用

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

in your get_all() . 在您的get_all()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM