【问题标题】:array_intersect(): Argument #1 is not an array?array_intersect():参数 #1 不是数组?
【发布时间】:2010-11-14 06:51:04
【问题描述】:

对于我的代码可能有什么问题,我已经没有什么想法了。这个特定的类接受一个数组,并根据另一个数组检查它以获得公共值。然后它通过 final_post_vars_keys() 函数提供对公共值的访问。但是每当我运行代码时,我都会收到错误(在标题中)。

 <?php

    class PostVarsKeys {
     private $general_keys = array("name", "email", "custom phone" , "lastname" , "firstname", "fname", "lname", "phone" , "emailaddress" ,  
            "phonenumber");
     private $post_vars_keys = array();


     public function __construct($post_keys){
      $counter=0;      
      foreach($post_keys as $key => $value):
       $this->post_vars_keys[$counter++] = $key;
      endforeach;
     }

     public function final_post_vars_keys(){
      return $final_keys = array_intersect($this->general_keys, $this->post_vars_keys);
     }
    }

【问题讨论】:

  • 我会在代码中搜索general_keys,看看它是否被用于其他可能导致值更改为非数组的地方。

标签: php array-intersect


【解决方案1】:

将参数转换为数组:

array_intersect((array)$this->general_keys, (array)$this->post_vars_keys);

【讨论】:

    【解决方案2】:

    $counter 变量在foreach 循环中每次都初始化为零。你试过把它拿出来吗?

    【讨论】:

    • 该死!是啊..但什么都没有改变。 :-(
    • 另外,作为旁注,这会更干净:foreach($post_keys as $key =&gt; $value) { $this-&gt;post_vars_keys[] = $key; }
    • cdhowie:$this-&gt;post_var_keys = array_keys($post_keys); 不是更干净吗?
    • 除了您指出的 $counter 之外,我的代码没有任何问题。这只是我的愚蠢 - 与代码无关。 :-) 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 2012-09-05
    • 2023-03-25
    • 1970-01-01
    • 2018-10-05
    相关资源
    最近更新 更多