Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带If的PHP Foreach循环_Php - Fatal编程技术网

带If的PHP Foreach循环

带If的PHP Foreach循环,php,Php,尝试循环通过包含项的结果。项目有一个类型,并尝试回显每个组的类别标题。通常情况下,这对一个循环非常有效,但我认为$item类型的foreach是一个很好的选择。有什么解决办法吗 <h2>Package <?=$packages->id?></h2> <?php foreach($packages->item as $item):?> <?php foreach($item->type as $type):?>

尝试循环通过包含项的结果。项目有一个类型,并尝试回显每个组的类别标题。通常情况下,这对一个循环非常有效,但我认为$item类型的foreach是一个很好的选择。有什么解决办法吗

<h2>Package <?=$packages->id?></h2>

<?php foreach($packages->item as $item):?>

    <?php foreach($item->type as $type):?>

        <?php $subtype = null;?>

        <?php if($subtype != $type->name)?>

            <h3><?=$type->name?></h3>

            <?=$item->name?><br>

            <?php $subtype = $type->name;?>

    <?php endforeach;?>

<?php endforeach;?>
我目前的结果是:

package 1
    category 1
        item 3
    category 1
        item 5
    category 2
        item 4
    category 2
        item 6
预期结果:

package 1
    category 1
        item 3
        item 5
    category 2
        item 4
        item 6

$subtype
没有角色可扮演,因为它在if语句之前被设置为Null

$subtype = null;
if($subtype != $type->name)  <------- This would always be true
$subtype=null;
如果($subtype!=$type->name)id);
foreach($packages->item as$item){
printf(“%s”,$type->name);
打印(
    ); foreach($item->type as$type){ printf(“
  • %s
  • ”,$item->name); } 打印(“
”); }
要解决上述问题,请参阅下面的代码片段

    <h2>Package <?=$packages->id?></h2> 
   <?php foreach($packages->item as $item):?> 
    <h3><?php echo $item->name;?></h3>

   <?php foreach($item->type as $type):?>
   <?php $subtype = null;?> 
   <?php if($subtype != $type->name)?> 
    <?=$type->name?><br> 
   <?php $subtype = $type->name;?> 
  <?php endforeach;?> 
  <?php endforeach;?>


你能
打印($packages->item)
而不是预先格式化它的大约1000行并将其粘贴到垃圾箱中吗。。要确定我在这里处理的是什么,您的代码中有语法错误,在您的条件下为什么要在每一行上打开和关闭PHP标记?这仍然给了我相同的结果,请使用我的pastebin检查我上面的注释。@dynamo让我处理您的真实对象,然后将
序列化($packages)
添加到过去的bin中。。我将能够直接处理您的对象。我发现我的代码工作正常,只是必须是一个php块,而不是打开/关闭每一行。我会接受的,因为你的代码帮我弄明白了。@dynamo很高兴知道。。。。但是如果不是这样的话,你不必写。。。您需要使代码干净易读
printf("<h2>%s</h2>", $packages->id);
foreach ( $packages->item as $item ) {
    printf("<h3>%s</h3>", $type->name);
    print("<ul>");
    foreach ( $item->type as $type ) {
        printf("<li>%s</li>", $item->name);
    }
    print("</ul>");
}
    <h2>Package <?=$packages->id?></h2> 
   <?php foreach($packages->item as $item):?> 
    <h3><?php echo $item->name;?></h3>

   <?php foreach($item->type as $type):?>
   <?php $subtype = null;?> 
   <?php if($subtype != $type->name)?> 
    <?=$type->name?><br> 
   <?php $subtype = $type->name;?> 
  <?php endforeach;?> 
  <?php endforeach;?>