简体   繁体   English

是否可以在不使用foreach的情况下循环数组?

[英]Is it possible to loop array without using foreach?

I have a html array structure.. 我有一个html数组结构。

I output this array with foreach loops. 我用foreach循环输出此数组。 (inside get_output functions) (在get_output函数内部)

Is it possible to output results without using foreach? 是否可以不使用foreach来输出结果?

$schema = array(
    array(
        'tag' => 'div',
        'class' => 'lines',
        array(
            'tag' => 'div',
             array(
                'tag' => 'span',
                'style' => 'margin:10px; padding:10px',
                'key' => '$key-countryname',
            ),
            'key' => '$value-country',
        ),
        array(
            'tag' => 'div',
             array(
                'tag' => 'span',
                'style' => 'margin:10px; padding:10px',
                'key' => '$key-countryname',
            ),
            'key' => '$value-country',
        ),
    )
);

My function is using foreach loops to output results 我的功能是使用foreach循环来输出结果

function get_output($schema, $t = -2){
    $t++; $tag = ""; $atts = array(); $keys = array(); $code = array();

    foreach($schema as $k => $v){        
        if(is_array($v)){
            $keys[] = get_output($v, $t);
        } else {
            switch($k){
                case "tag": $tag = $v; break;
                case "key": $keys[] = $v; break;
                case "type": break;
                default: $atts[$k] = $v; break;
            }
        }    
    }
    if(0 < $t){ $code[] = "\n".str_repeat("\t", $t); }
    if($tag){
        $code[] = "<$tag"; foreach($atts as $k=>$v){ $code[] = ' '.$k.'="'.$v.'"'; } $code[] = ">";
        $code = array(implode('', $code));
    }
    foreach($keys as $k){ $code[] = $k; } 
    if($tag){
        $code[] = "\n".str_repeat("\t", $t);
        $code[] = '</'.$tag.'>'; 
    }
    //print_r($code);
    return implode("", $code);
}

while and for are perfectly valid ways to loop array: whilefor是循环数组的完美有效方法:

$a = array(1,2,3); // indexed
$b = array(
    'a' => 1,
    'b' => 2,
    'c' => 3
); // associative

echo '$a indexed with "for": <br />';
for ($i = 0; $i < count($a); $i++) {
   echo $a[$i] . '<br />';
}
echo '$a indexed with "while": <br />';
$i = 0; // reset counter
while ($i < count($a)) {
   echo $a[$i] . '<br />';
   $i++;
}

echo '$b assoc with "for": <br />';
for ($i = 0; $i < count($a); $i++) {
   echo key($b) . ' => ' . current($b) . '<br />';
   next($b); // step forward
}
echo '$b assoc with "while": <br />';
reset($b); // rewind array cursor to start
while ($value = current($b)) {
   echo key($b) . ' => ' . $value . '<br />';
   next($b); // step forward
}

Also as you already implemented in your second code snippet, multi-dim array can be looped with recursion (though compared to nested loops it consumes more memory) 同样,正如您已经在第二个代码段中实现的那样,可以通过递归循环多维度数组(尽管与嵌套循环相比,它消耗更多的内存)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM