打破内部foreach循环但不是所有循环

时间:2016-05-03 03:11:58

标签: php foreach break

如何在PHP中打破嵌套的foreach循环?或者至少我认为这就是我需要做的事情。基本上我有一个像这样的项目列表

Item
|->Tag
|->Tag
Item
|->Tag
|->Tag
|->Tag->Tag has image

一组物品。每个项目都有一些标签,其中一些标签有一个图标图像。我想循环遍历项目,然后是项目中的标签,如果有图标则使用它,如果不是留空。这是我所拥有的,但它设置了所有具有图像的项目,甚至我知道没有带图标的标签。我假设在嵌套break循环中我可能需要foreach?当我尝试它似乎使东西更加关闭。

<?php foreach ($stories as $story) {

    $title = $story['content_name'];
    $desc = $story['content_social_description'];
    $image = $story['content_image'];
    $story_url = $story['content_slug'];
    $story_id = $story['content_id'];

    $tags = $story['tags'];

    foreach ($tags as $tag) {
        $tag_image = $tag['tag_image'];

        if($tag_image !== ''){
            $story_icon = $tag['tag_image'];
            //add break here? 
        }

        if ($tag['tag_type'] == 3) {
            $category = $tag['tag_name'];
        }

    }

    if( strlen($story_url) < 3){
        $story_slug = $story_id;
    }else{
        $story_slug = $story_url;
    }
?>

当我遍历标签时,这是一个项目的样子。同样,一些项目将具有没有与之关联的图标的标签。

array(4) {
  [0]=>
  array(10) {
    ["tag_id"]=>
    string(1) "1"
    ["tag_name"]=>
    string(8) "Featured"
    ["tag_score"]=>
    string(4) "2650"
    ["tag_type"]=>
    string(1) "1"
    ["tag_image"]=>
    string(0) ""
    ["tag_teamcount"]=>
    string(1) "0"
    ["tag_teamcolor"]=>
    string(0) ""
    ["tag_teamsocialprefix"]=>
    string(0) ""
    ["content_id"]=>
    string(1) "1"
    ["type_name"]=>
    string(5) "Admin"
  }
  [1]=>
  array(10) {
    ["tag_id"]=>
    string(1) "4"
    ["tag_name"]=>
    string(11) "Clean Water"
    ["tag_score"]=>
    string(1) "0"
    ["tag_type"]=>
    string(1) "3"
    ["tag_image"]=>
    string(0) ""
    ["tag_teamcount"]=>
    string(1) "0"
    ["tag_teamcolor"]=>
    string(9) "undefined"
    ["tag_teamsocialprefix"]=>
    string(9) "undefined"
    ["content_id"]=>
    string(1) "1"
    ["type_name"]=>
    string(7) "Explore"
  }
  [2]=>
  array(10) {
    ["tag_id"]=>
    string(2) "11"
    ["tag_name"]=>
    string(5) "Fast"
    ["tag_score"]=>
    string(4) "1400"
    ["tag_type"]=>
    string(1) "2"
    ["tag_image"]=>
    string(17) "TagIcon.png"
    ["tag_teamcount"]=>
    string(1) "0"
    ["tag_teamcolor"]=>
    string(0) ""
    ["tag_teamsocialprefix"]=>
    string(0) ""
    ["content_id"]=>
    string(1) "1"
    ["type_name"]=>
    string(9) "Exchanges"
  }
  [3]=>
  array(10) {
    ["tag_id"]=>
    string(2) "12"
    ["tag_name"]=>
    string(12) "Sub Featured"
    ["tag_score"]=>
    string(4) "3750"
    ["tag_type"]=>
    string(1) "1"
    ["tag_image"]=>
    string(0) ""
    ["tag_teamcount"]=>
    string(1) "0"
    ["tag_teamcolor"]=>
    string(0) ""
    ["tag_teamsocialprefix"]=>
    string(0) ""
    ["content_id"]=>
    string(1) "1"
    ["type_name"]=>
    string(5) "Admin"
  }
}

基本上每个项目扫描所有标签。如果有与标签关联的图像,则设置变量并转到下一个项目。如果没有图像,请不要为该项目设置变量,并在扫描完所有项目标签后继续。

1 个答案:

答案 0 :(得分:0)

我认为你需要做这样的事情。

  1. 重置story_icon和类别
  2. 在每个循环中断的内部使用$category

    <?php foreach ($stories as $story) {
    
    $title = $story['content_name'];
    $desc = $story['content_social_description'];
    $image = $story['content_image'];
    $story_url = $story['content_slug'];
    $story_id = $story['content_id'];
    
    $tags = $story['tags'];
    
    //reset the story_icon and the category
    $category = '';
    $story_icon = '';
    
    foreach ($tags as $tag) {
        $tag_image = $tag['tag_image'];
    
        if ($tag['tag_type'] == 3) {
            $category = $tag['tag_name'];
        }
    
        if($tag_image !== ''){
            $story_icon = $tag['tag_image'];
            break;
        }
    }
    
    if( strlen($story_url) < 3){
        $story_slug = $story_id;
    }else{
        $story_slug = $story_url;
    }
    ?>
    
  3.   

    试试这个,让我知道问题出在哪里。