Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts

    Accessing the second level of a Multidimensional array

    Ok, so I have a multidimensional array:
    PHP Code:
     
    $fields 
    = array(
        
    'username' => 
                        array(
    "isEmpty","checkUsername"), 
        
    'password' =>
                        array(
    "isEmpty","checkPassword"),
    ); 
    And what I am trying to do is get the second array and do a foreach with them. I am trying to create my formValidation class. The idea is that I want to be able to list the fields in an array then rules in an array within it.

    Then, when I call my validation class its construct method:
    PHP Code:
    function __construct($fields) {
            
    $this->getRules($fields);
            if(!
    $this->isValid) {
            return 
    $this->errors;    
            } 

    which calls a method getRules(); this is the get rules method:
    PHP Code:
    private function getRules($fields) {
            foreach (
    $fields as $field) {
               if(
    array_key_exists($field$_POST)){
                    foreach (
    $field as $rule) {
                        if(
    method_exists($this,$rule)) {
                            
    $this->$rule($field);    
                        }else {
                            
    $this->errors["method"] = "This rule does not exist";    
                        }
                    }
               } else {
                    
    $this->errors["exist"] = "This field does not exist"
               }
            }
            return 
    $this->errors;

    which calls the methods of the rules.

    But this isn't exactly working. I'm getting the error=> array_key_exists:The first argument should be either a string or an integer
    Last edited by Chris Hick; 01-14-2012 at 03:26 PM.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #2
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    I figured out the solution to this problem. Definitely took me some thinking, but here is the solution.
    PHP Code:
    private function getRules($fields) { 
            foreach (
    $fields as $field=>$rules) { 
               if(
    array_key_exists($field$_POST)){ 
                    foreach (
    $rules as $rule) { 
                        if(
    method_exists($this,$rule)) { 
                            
    $this->$rule($field);     
                        }else { 
                            
    $this->errors["method"] = "This rule does not exist";     
                        } 
                    } 
               } else { 
                    
    $this->errors["exist"] = "This field does not exist";  
               } 
            } 
            return 
    $this->errors

    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •