views:

408

answers:

5
// a beautiful multidimensional array

public $form = array (
        array(
            'field' => 'email',
            array(
                'params' => 
                array(
                    'rule' => 'email',
                    'on' => 'create',
                    'required' => true,
                    ),
                ),
            array(
                'params' =>
                array(
                    'rule' => 'email',
                    'on' => 'update',
                    'required' => false,
                    )
                )
            )
        );

// beautiful foreach loops

public function validate($form) {
    foreach ($form as $valueA) {
        $field = $valueA['field'];

        foreach ($valueA as $valueB) {
            $params = $valueB['params'];

            foreach ($valueB as $valueC) {
                $rule = $valueC['on'];
                $on = $valueC['on'];
                $required = $valueC['required'];

                $this->isValid($field, $rule, $on, $required);
            }
        }
    }
}

// they do not work together!!!
A: 

This page has some examples of using for loops to access elements of a multidimensional array: http://www.webcheatsheet.com/PHP/multidimensional_arrays.php

It is missing foreach loops for the three-dimensional array :-(
Well, you could have some variable that counts the rows as they are entered in the array and do the for loop until that point. I'm not an expert at PHP, but I would think you should be able to do something similar.
A: 

As "beautiful" as those arrays are, sure looks to me like something a class would be better at, and it'd be a lot easier to process.

Chad Birch
A: 

That multidimensional array doesn't look beautiful to me. It looks like a mess. This seems much more logical:

public $form = array (
            'field' => 'email',
            'params' => 
              array(
                array(
                    'rule' => 'email',
                    'on' => 'create',
                    'required' => true,
                    ),
                array(
                    'rule' => 'email',
                    'on' => 'update',
                    'required' => false,
                    )
                )
        );
McAden
It is because there are going to be multiple 'field's and 'param's, so they have to be in separated arrays.
A: 

It looks to me like you're going to generate errors in your second loop:

foreach ($ValueA as $ValueB) {

This is going to include field in the loop and will encounter problems when it tries to access it as an array.

Also, I think you mean for your third loop to be:

foreach ($params as $ValueC) {

Otherwise, it runs into the same problems as the middle loop.

I think that, if you intend to keep using this as an array rather than refactor it into a class as others have suggested, you should restructure it so that the named data is all at the same level. Notice that this reduces the complexity of both the array (a little) and the loops (a lot).

public $form = array (
    array(
        'field' => 'email',
        'params' => array(
            array(
                'rule' => 'email',
                'on' => 'create',
                'required' => true,
            ),
            array(
                'rule' => 'email',
                'on' => 'update',
                'required' => false,
            )
        )
    )
);

public function validate($form) {
    foreach ($form as $field_params) {
        $field = $field_params['field'];

        foreach ($field_params['params'] as $param) {
            $this->isValid($field, $param['rule'], $param['on'], $param['required']);
        }
    }
}
Ben Blank
A: 

Try to debug what is $valueX in your case with var_dump() for example.

May be foreach($array as $key => $value) is what do you looking for

kingoleg