【问题标题】:Wrong data type in in_array function in kohana framework?kohana 框架中的 in_array 函数中的数据类型错误?
【发布时间】:2012-09-02 14:04:35
【问题描述】:

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

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_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;

}

}

但是 Kohana 给我一个错误:

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

怎么了?

【问题讨论】:

  • 您可以执行var_dump($webs) 并查看该变量中的数据。我猜有一个对象,而不是数组。
  • 是的,有一个对象:object(Database_MySQL_Result)#24 (7) { ["_internal_row:protected"]=&gt; int(0) ["_query:protected"]=&gt; string(18) "SELECT * FROM webs" ["_result:protected"]=&gt; resource(73) of type (mysql result) ["_total_rows:protected"]=&gt; int(5) ["_current_row:protected"]=&gt; int(0) ["_as_object:protected"]=&gt; bool(false) ["_object_params:protected"]=&gt; NULL }

标签: php kohana


【解决方案1】:

您需要数组变量而不是 Database_Result 对象:

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

【讨论】:

    【解决方案2】:

    Models返回的数据,即Database_MySQL_Result,需要先改成数组。 你可以使用

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

    在你的get_all()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 2019-10-19
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 2012-09-27
      相关资源
      最近更新 更多