[英]Is there a way to infinitely step deeper into an array
i have the following code 我有以下代码
foreach($arrayData as $_key_l1 => $_data_l1)
{
if(is_array($_data_l1))
{
}
else
{
switch($_data_l1)
{
case '':
$arrayData[$_key_l1] = null;
break;
}
}
}
the idea is that when $arrayData
is passed into the loop, if any element in the array is itself an array it'll step deeper, using the code above, i would copy that into the if
and replace the _l1
in the variables with _l#
where # is the level i'm on, i would also add another [$_key_l#]
想法是,当$arrayData
传递到循环中时,如果数组中的任何元素本身就是数组,则使用上面的代码,它会更深入,我将其复制到if
,并将变量中的_l1
替换为_l#
其中#是我所在的级别,我还要添加另一个[$_key_l#]
at the moment i don't expect it to be any deeper than 3 levels however i would like it if it could go though the array without me having to add in code for each level of depth, so i am wondering, is there already a way do to what i'm trying to do but more dynamicly 目前我不希望它的深度超过3个级别,但是我希望它可以通过数组而无需我为每个深度级别添加代码,所以我想知道是否已经有一个方式去做我想做的事,但更动态
NOTE: the switch ... case
is incomplete, so far it just turns blank strings into null 注意: switch ... case
不完整,到目前为止,它只是将空白字符串变为null
You can use recursive function calls. 您可以使用递归函数调用。
Example: 例:
function step_array(&$arr) {
foreach ($arr as $key => $val) {
if (is_array($val)) {
step_array($val);
} else {
// deal with the value
}
}
}
you can use the built in function of php named array_walk_recursive() 您可以使用名为array_walk_recursive()的php内置函数
here is the tutorial link 这是教程链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.