Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.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 herdoc函数中的循环_Php_Wordpress - Fatal编程技术网

Php herdoc函数中的循环

Php herdoc函数中的循环,php,wordpress,Php,Wordpress,也许这已经被问了很多次,但似乎找不到合适的。我是php新手,正在尝试做一些疯狂/错误的事情 是wordpress。我有一个可以重用的函数。我需要使用函数,因为我传递参数。在该函数中,我循环到echoout html,但不断得到“注意数组到字符串的转换” 函数foo($bar){ //我不知道正确的方法! foreach($bar作为$bar){ $big_bar=$bar; } echo有更多的方法可以做到这一点。如果您想回显$bar中的每个值: function foo($bars) {

也许这已经被问了很多次,但似乎找不到合适的。我是php新手,正在尝试做一些疯狂/错误的事情

是wordpress。我有一个可以重用的函数。我需要使用函数,因为我传递参数。在该函数中,我循环到
echo
out html,但不断得到“注意数组到字符串的转换”

函数foo($bar){
//我不知道正确的方法!
foreach($bar作为$bar){
$big_bar=$bar;
}

echo有更多的方法可以做到这一点。如果您想回显
$bar
中的每个值:

function foo($bars) {
  foreach ($bars as $bar) {
    echo <<<HTML
<div>
$bar
</div>
HTML;
  }
}
函数foo($bar){
foreach($bar作为$bar){

echo我认为您可能希望获得这样的输出,或者这对您有帮助

    function foo($bars) 
    {
        $big_bar = '';

        foreach ($bars as $bar) {
            $big_bar .= $bar;
         }

         echo "<div>'".$big_bar."'</div>";
    }
否则,您将得到错误的

注意:数组到字符串的转换


.

可以在heredoc内部循环,在那里

<?php
$map=function($a,$f){return join("\n",array_map($f,$a));};
$data=[1,2,3];

$select = <<<HERE
<select>
 {$map($data,function($item){return"
  <option>$item</option>
 ";})}
</select>
HERE;

print $select;

$bar
的值是多少?@Fred ii-它只是另一个数组。“它只是另一个数组”–不,它可能是一个多维数组,因为只有这样错误信息才会在这里有意义。正如@CBroe所说,它可能是数组的数组,这就是为什么会出现这样的错误。很抱歉延迟。不是数组的数组,只是
$bar=[1,2,3,4]
但下面的答案奏效了。我仍在努力了解php,很抱歉我提出了一个愚蠢的问题。这个练习可能已经被简化,以消除一些不必要的东西,他们可能仍然想知道如何做,特别是当问题的措辞有点糟糕时,这是唯一真正回答这个问题的答案。
    function foo($bars) 
    {
        $big_bar = '';

        foreach ($bars as $bar) {
            $big_bar .= $bar;
         }

         echo "<div>'".$big_bar."'</div>";
    }
$bars = array("0"=>"first","1"=>"second");
<?php
$map=function($a,$f){return join("\n",array_map($f,$a));};
$data=[1,2,3];

$select = <<<HERE
<select>
 {$map($data,function($item){return"
  <option>$item</option>
 ";})}
</select>
HERE;

print $select;
<?php
$map=function($a,$f){return join("\n",array_map($f,$a));};
$data=[1,2,3];

$select = "
<select>
 {$map($data,function($item){return"
  <option>$item</option>
 ";})}
</select>
";

print $select;