Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.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
PHP foreach循环帮助_Php_Loops_Foreach - Fatal编程技术网

PHP foreach循环帮助

PHP foreach循环帮助,php,loops,foreach,Php,Loops,Foreach,我使用的是一个PHP foreach循环,我需要它根据输出的数组值输出一些特定的HTML 每次foreach循环命中$content['contentTitle'],我都需要它插入一个新表,然后从那里继续。本质上,我希望for循环在每次找到新的contentTitle时吐出一个新表,然后将数组中的其余数据添加为tr和td。那么,在循环中添加一个if条件。如果遇到$content['contentTitle'],则关闭上一个表并启动一个新表。假设$content是某个父数组的元素,并且该父数组中的

我使用的是一个PHP foreach循环,我需要它根据输出的数组值输出一些特定的HTML

每次foreach循环命中$content['contentTitle'],我都需要它插入一个新表,然后从那里继续。本质上,我希望for循环在每次找到新的contentTitle时吐出一个新表,然后将数组中的其余数据添加为tr和td。

那么,在循环中添加一个if条件。如果遇到$content['contentTitle'],则关闭上一个表并启动一个新表。

假设$content是某个父数组的元素,并且该父数组中的第一个条目确实有一个contentTitle条目,则可以执行以下操作:

foreach($content as $key=>$value){
  if($key == "contentTitle"){
    echo "<table>";
  }
  else{
    echo "<tr><td>";
  }
}
$first = true;
echo "<table>\n"; // start initial table
foreach ($stuff as $content) {
    if ($content['contentTitle']) {
        if (!$first) {
           // If this is NOT the $first run through, close the previous table
           $first = false; //... and set the sentinel to false
           echo "</table>\n";
        }
        // output the contentTitle
        echo <<<EOF
<h1>{$content['contentTitle']}</h1>

<table>

EOF;
    } else {
        // otherwise output the regular non-title content
        echo <<<EOF
<tr>
    <td>{$content['this']}</td>
    <td>{$content['that']}</td>
    <td>{$content['somethingelse']}</td>
</tr>

EOF;
   }
}

你能把你的问题改写一下吗?这完全不可理解。另外,请提供一些代码或至少预期的输入/输出。提示没有数组,并且交互不是可选的。-1因为从您的问题中可以看出的唯一一点是您使用的是基于表的布局。学习编写HTML,然后学习编写PHP。