Page 1 of 1

Multidimensional Associative Named Key Arrays

Posted: Wed Aug 06, 2008 6:15 pm
by aspekt9
A lot to take in with that title I know, what I'm trying to do I don't believe to be too complicated but I'm having a hard time understanding something. Is what I'm trying to do is create a multidimensional array of data that I will use to dynamically call a function using:

Code: Select all

call_user_func_array($function, $variables)
Now here's how I have my array setup.

I have 3 main named keys (representing the name of the functions):

Code: Select all

string, numeric, username.
In these arrays I have the variables that will be passed to each one of those functions:

username which I need to take 2 additional parameters that I use another nested array to take care of.
password which I also need to take 2 additional parameters.
confirmPassword which I need 2 additional parameters for.

So I need it to look something like this when all the code is done computing:

Code: Select all

call_user_func_array(string($username, @param1, @param2));
 call_user_func_array(string($password, @param1, @param2));
 call_user_func_array(string($confirmPassword, @param1, @param2));
 call_user_func_array(string($firstName));
 call_user_func_array(string($lastName));
 call_user_func_array(numeric($memberLevel));
 call_user_func_array(numeric($memberId));
 call_user_func_array(username($username));
 
Here's the code with the data and the functions and variables:

Code: Select all

       $data = array (
                'username'        => 'username',
                'password'        => md5('pass'),
                'confirmPassword' => md5('pass'),
                'firstName'       => 'firstname',
                'lastName'        => 'lastname',
                'memberLevel'     => '2',
                'memberId'        => '4',
        );
        $val_array = array(
                'string'            => array(
                    array('username', 3, 4), 
                    array('password', 4, 3), 
                    array('confirmPassword', 4, 3), 
                    'firstName', 'lastName'),
                'numeric'           => array('memberLevel', 'memberId'),
                'username'          => array('username'),
            );
        $error = array();
        
        foreach ($val_array as $var => $val_err) {
            
            // if (is_array($val_array[$var])) {
            // }
            $function = $var;
            
            foreach ($val_err as $var) {
            
                //$error[] = $var;
                echo "validate_".$function."(".$data[$var].")\n";
            }
        }
Am I making any sense to anybody? Thanks guys.

Re: Multidimensional Associative Named Key Arrays

Posted: Wed Aug 06, 2008 6:44 pm
by manixrock
Try something like this:

Code: Select all

$val_array = array(
     'username' => array('string', 3, 4),
     'username' => array('username'),
     'password' => array('string', 4, 3),
     'confirmPassword' => array('string', 4, 3),
     'firstName' => array('string'),
     'lastName' => array('string'),
     'memberLevel' => array('numeric'),
     'lastName' => array('numeric'),
);
 
foreach($val_aray as $name => $args) {
    $type = $args[0];
    $args[0] = $data[$name];
    call_user_func_array("validate_$type", $args);
}

Re: Multidimensional Associative Named Key Arrays

Posted: Wed Aug 06, 2008 8:10 pm
by aspekt9
Here's what I used to test it and it seems to pull the the arguments and function names correctly, It doesn't seem to like the duplicate username key, if I remove the second one with just username => array('username') it will print the username key above it, however if I leave it in it doesn't print either. Also, lastname only has a 2 after it where is the 2 coming from?

Screen output (errors and echos):

Code: Select all

username1a1dc91c907325c69271ddf0c944bc72
4
3
1a1dc91c907325c69271ddf0c944bc72
4
3
firstname
0
32
lastname2

New code:

Code: Select all

       $data = array (
                'username'        => 'username',
                'password'        => md5('pass'),
                'confirmPassword' => md5('pass'),
                'firstName'       => 'firstname',
                'lastName'        => 'lastname',
                'memberLevel'     => '2',
                'memberId'        => '4',
        );
    $val_array = array(
          'username' => array('string', 3, 4),
          'username' => array('username'),
          'password' => array('string', 4, 3),
          'confirmPassword' => array('string', 4, 3),
          'firstName' => array('string', 0, 32),
          'lastName' => array('string', 0, 32),
          'memberLevel' => array('numeric'),
          'lastName' => array('numeric'),
    );
  
        foreach($val_array as $name => $args) {
            $type = $args[0];
            $args[0] = $data[$name];
            call_user_func_array("validate_$type", $args);
        }
         
        function validate_string($str, $i, $j) {
            echo $str . "<br />";
            echo $i . "<br />";
            echo $j . "<br />";
        }
         
        function validate_numeric($num) {
          echo $num;
        }
        
        function validate_username($str) {
            echo $str;
        }