array_intersect():参数#1不是数组?

时间:2010-11-14 06:51:04

标签: php array-intersect

我的代码可能出错了。此特定类接受一个数组并将其与另一个数组进行检查以获取公共值。然后它通过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);
     }
    }

2 个答案:

答案 0 :(得分:3)

将参数转换为数组:

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

答案 1 :(得分:1)

每次在$counter循环中,

foreach变量都被初始化为零。你试过把它拿出来吗?