dongyi5425
2010-11-14 06:51
浏览 71
已采纳

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

I am running out of ideas as to what could be wrong with my code. This particular class accepts an array and checks it against another array to get the common values. Then it provides access to the common values thru final_post_vars_keys() function. But I get the error(in the title) whenever I run the code.

 <?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);
     }
    }

图片转代码服务由CSDN问答提供 功能建议

我的代码可能出错了。 此特定类接受一个数组并将其与另一个数组进行检查以获取公共值。 然后它通过final_post_vars_keys()函数提供对公共值的访问。 但每当我运行代码时,我都会收到错误(在标题中)。

 &lt;?php 
 
 class PostVarsKeys {
 private $ general_keys = array(“name”,“email”,“custom phone”,“lastname”,  “firstname”,“fname”,“lname”,“phone”,“emailaddress”,
“phonenumber”); 
 private $ post_vars_keys = array(); 
 
 
公共函数__construct($ post_keys)  {
 $ counter = 0;  
 \ foreach($ post_keys as $ key =&gt; $ value):
 $ this-&gt; post_vars_keys [$ counter ++] = $ key; 
 endforeach; 
} 
 
公共函数final_post_vars_keys(){\  n return $ final_keys = array_intersect($ this-&gt; general_keys,$ this-&gt; post_vars_keys); 
} 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanmengsuo9302 2010-11-14 06:57
    已采纳

    $counter variable is initialized to zero every time in the foreach loop. Have you tried taking it out?

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题