如何在echo中使用foreach来回显数组

时间:2018-09-02 02:59:03

标签: php html foreach echo

我有一个数组{ bool[] boolArray = new bool[5]; boolArray[1] = true; QuizzerEngine.TestMethod15(boolArray); Console.WriteLine("After TM15: {0},{1},{2},{3},{4}\n", boolArray[0], boolArray[1], boolArray[2], boolArray[3], boolArray[4]); } { bool[] boolArray = new bool[5]; boolArray[1] = true; QuizzerEngine.TestMethod16(boolArray); Console.WriteLine("After TM16: {0},{1},{2},{3},{4}\n", boolArray[0], boolArray[1], boolArray[2], boolArray[3], boolArray[4]); } { bool[] boolArray = new bool[5]; boolArray[1] = true; QuizzerEngine.TestMethod17(boolArray); Console.WriteLine("After TM17: {0},{1},{2},{3},{4}\n", boolArray[0], boolArray[1], boolArray[2], boolArray[3], boolArray[4]); } { bool[] boolArray = new bool[5]; boolArray[1] = true; QuizzerEngine.TestMethod18(boolArray); Console.WriteLine("After TM18: {0},{1},{2},{3},{4}\n", boolArray[0], boolArray[1], boolArray[2], boolArray[3], boolArray[4]); } { bool[] boolArray = new bool[5]; boolArray[1] = true; QuizzerEngine.TestMethod18(boolArray); Console.WriteLine("After TM19: {0},{1},{2},{3},{4}\n", boolArray[0], boolArray[1], boolArray[2], boolArray[3], boolArray[4]); }

如何使用Inside Test Method 15. Printing bool array param: 0,1,0,0,0, Putting values in the bool array: 1,0,1,0,1, After TM15: False,True,False,False,False Inside Test Method 15. Printing bool array param: 0,1,0,0,0, Putting values in the bool array: 1,0,1,0,1, After TM16: False,True,False,False,False Inside Test Method 15. Printing bool array param: 0,0,0,0,1, Putting values in the bool array: 1,0,1,0,1, After TM17: False,True,False,False,False Inside Test Method 15. Printing bool array param: 0,0,255,255,0, Putting values in the bool array: 1,0,1,0,1, After TM18: False,True,False,False,False Inside Test Method 15. Printing bool array param: 0,0,255,255,0, Putting values in the bool array: 1,0,1,0,1, After TM19: False,True,False,False,False 输出此内容?

下面的代码有错误:

$category_slugs

谢谢

4 个答案:

答案 0 :(得分:1)

您不能在echo中使用foreach。要实现您要尝试的功能,可以使用implode函数,该函数将字符串数组的值连接起来。

echo "<div class='transition " .implode(' ', $category_slugs). "' data-category='transition'> " ?>

答案 1 :(得分:0)

尝试此代码

<div class="transition <?php foreach($category_slugs as $slug){echo $slug . ' ';} ?>" data-category="transition">
  

串联运算符('。'),返回其左右参数的串联。

来自php manual

但是此代码不是字符串。

foreach($category_slugs as $slug){echo $slug;echo ' ';}

所以您会得到一个错误。

本手册可以Escaping from HTML为您提供帮助。

对不起,我的英语不好,希望对您有帮助。

答案 2 :(得分:0)

像这样将<div>包装在for循环中

<?php

$slug_string = "";

foreach($category_slugs as $slug){

$slug_string .= $slug_string." ";

}

echo "<div class='transition ".$slug_string."' data-category='transition'> ";

?>

答案 3 :(得分:0)

尝试

$category_slugs =array('a','b','c');
foreach($category_slugs as $slug){
echo "<div class='transition ".$slug.' '."' data-category='transition'></div> " ;
}