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);
     }
    }
1
  • I would search the code for general_keys and see if it's used anywhere else that could cause the value to change to something that isn't an array. Commented Nov 14, 2010 at 7:25

2 Answers 2

5

Cast the arguments as arrays:

array_intersect((array)$this->general_keys, (array)$this->post_vars_keys);
Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

dang! yah.. but nothing's changed. :-(
Also, as a side note, this would be cleaner: foreach($post_keys as $key => $value) { $this->post_vars_keys[] = $key; }
cdhowie: Wouldn't just $this->post_var_keys = array_keys($post_keys); be even cleaner?
aside from the $counter you pointed out, there was nothing wrong with my code.. It's just stupidity on my part--nothing to do with the code. :-) Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.