Question

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

Solution

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

OTHER TIPS

Cast the arguments as arrays:

array_intersect((array)$this->general_keys, (array)$this->post_vars_keys);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top