// 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!!!
5 个解决方案
#1
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']);
}
}
}
#2
This page has some examples of using for loops to access elements of a multidimensional array: http://www.webcheatsheet.com/PHP/multidimensional_arrays.php
此页面包含一些使用for循环访问多维数组元素的示例:http://www.webcheatsheet.com/PHP/multidimensional_arrays.php
#3
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.
正如那些数组一样“美丽”,我肯定会认为类似于一个类更好的东西,而且处理起来要容易得多。
#4
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,
)
)
);
#5
Try to debug what is $valueX
in your case with var_dump()
for example.
例如,尝试使用var_dump()来调试$ valueX。
May be foreach($array as $key => $value)
is what do you looking for
可能是foreach($ array为$ key => $ value)是你在寻找什么
#1
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']);
}
}
}
#2
This page has some examples of using for loops to access elements of a multidimensional array: http://www.webcheatsheet.com/PHP/multidimensional_arrays.php
此页面包含一些使用for循环访问多维数组元素的示例:http://www.webcheatsheet.com/PHP/multidimensional_arrays.php
#3
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.
正如那些数组一样“美丽”,我肯定会认为类似于一个类更好的东西,而且处理起来要容易得多。
#4
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,
)
)
);
#5
Try to debug what is $valueX
in your case with var_dump()
for example.
例如,尝试使用var_dump()来调试$ valueX。
May be foreach($array as $key => $value)
is what do you looking for
可能是foreach($ array为$ key => $ value)是你在寻找什么