0

这段代码应该解释一切..

foreach($this->sections as $k => $section){ 

    $preview = array();

    foreach ($section['fields'] as $k => $type) {                               
       $preview[] = $type['type'];
    }

    if ($preview == 'header_preview'){
        echo $preview;
        // I will loop content here based on the $section loop NOT the $type loop
    }

}

我只需要$section['fields']在该循环之外获取每个然后,它再次获取所有$section['fields']然后使用这些字段类型之一来创建 if 语句的列表。以上不起作用,我将在这里向您展示工作代码。

foreach($this->sections as $k => $section){ 

    foreach ($section['fields'] as $k => $type) {                               
        if ($type['type'] == 'header_preview'){
           //I work but im in a nested loop I need out
        }
     }

  //The main loop here.. the above loop is just to setup data to use inside this loop? Make sense? I hope!

}

我希望这是有道理的...

var_dump 的片段$this->sections

array(26) { ["general"]=> array(3) { ["title"]=> string(7) "General" ["icon"]=>    string(106) "img/icons/sub.png" ["fields"]=> array(5) { [0]=> array(6) { ["id"]=>    string(10) "responsive" ["type"]=> string(6) "switch" ["title"]=> string(35) "Responsive" ["desc"]=> string(10) "Responsive" ["options"]=> array(2) { [1]=> string(2) "On"    [0]=> string(3) "Off" }
4

3 回答 3

1

嗯……也许

foreach($this->sections as $k => $section){ 

    $preview = array();

    foreach ($section['fields'] as $x => $type) {                               
       $preview[] = $type['type'];
    }

    if(!in_array('header_preview', $preview)){

        // Here $preview DOES NOT contain 'header_preview'
        // Do stuff

    } 
}
于 2013-08-24T14:57:03.647 回答
1

可能 $k 已被复制,因此循环不知道该做什么。您可以尝试将 $k 更改为 $x,看看是否有效。

foreach($this->sections as $k => $section){ 

    $preview = array();

    foreach ($section['fields'] as $x => $type) {                               
       $preview[] = $type['type'];
    }

    foreach($preview as $elem){
        if ($elem == 'header_preview'){
            echo($elem);
        }
    }
}
于 2013-08-24T14:39:03.847 回答
1
$header_preview=false;
foreach($this->sections as $k => $section){ 
    $header_preview=false;// reset the var $header_preview
    foreach ($section['fields'] as $k => $type) {                               
        if ($type['type'] == 'header_preview') $header_preview = true;
    }
    if($header_preview==true){
        //here you can add or extract from this array that has `header_preview` value for one of it's field type
    }
}
于 2013-08-24T15:10:57.113 回答