[英]how inserting foreach loop all final result in a variable in php?
如何在 php.ini 中插入 foreach 循环所有最终结果中的变量。
例如我有三个数组:
$myarray = ('a','b','c');
foreach($myarray as $myarray){
$text = $myarray;
}
echo $text;//i want echo abc but this code only print c.
我想要将所有最终代码打印成类似 $text 并回显循环。
我知道这段代码很好用,但我希望所有的结果都在循环之外
$myarray = ('a','b','c');
foreach($myarray as $myarray){
$text = $myarray;
echo $text;
}
请帮忙。
在此先感谢所有朋友。
这可能是您正在寻找的:
<?php
$myarray = ('a','b','c');
$text = '';
foreach($myarray as $element){
$text .= $element;
}
echo $text;
另一种选择是这样的:
<?php
$myarray = ('a','b','c');
echo implode('', $myarray);
$text = [];
$myarray = ['a','b','c'];
foreach($myarray as $element) {
$text[] = $element; // append a new element to the array $text
// ok, kinda useless since $text==$myarray after the loop
// but ....it's only an example
}
echo join(', ', $text); see // http://docs.php.net/join
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.