How can I make this multidimensional array and these foreach loops work together?

// 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!!!

      

+1


a source to share


5 answers


It looks to me like you are going to generate errors in the second loop:

foreach ($ValueA as $ValueB) {

      

This will include field

in a loop and will run into problems when trying to access it as an array.

Also, I think you mean that for your third loop:



foreach ($params as $ValueC) {

      

Otherwise, it faces the same problems as the middle cycle.

I think that if you intend to continue to use this as an array rather than refactoring it into a class as others have suggested, you should rebuild it so that the named data is at the same level. Note that this reduces the complexity of both the array (slightly) and 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']);
        }
    }
}

      

0


a source


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



0


a source


As "pretty" as these arrays are, they probably look to me as a class that will be better and much easier to handle.

0


a source


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

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

      

0


a source


Try to debug $valueX

in your case with var_dump()

eg.

Maybe foreach($array as $key => $value)

this is what you are looking for

0


a source







All Articles