基本内爆foreach [英] Basic implode foreach
问题描述
我有下面的代码,我想回显用逗号分隔的数组元素。代码输出错误列表,但没有逗号。我错过了什么?
$ $ p $ <?php
$ array = get_field('casts');
$ elements = $ array;
foreach($ array as $ key => $ value){
echo implode(',',$ value)};
?>
编辑1:其中 编辑2: pre class =lang-php prettyprint-override> 为什么要分配 不需要循环( 试试这个: 以下是 玩游戏并测试上面的代码这里。 I have the following code of which I want to echo array elements separated by commas. The code outputs the disered list, but without commas. What am I missing?
EDIT 1: where EDIT 2: Working snippet:
Why are you assigning Also you don't need to loop ( Try this: Here is the documentation on You can play around and test the above code here. Also next time, add the tag 这篇关于基本内爆foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! code>是嵌套数组。
<?php
$ array = get_field('casts');
$ new_array = array();
foreach($ array as $ sub_array){
foreach($ sub_array as $ value){
array_push($ new_array,$ value);
echo implode(,,$ new_array);
?>
$ elements = $ array;
然后从不使用 $ elements
?
foreach
)来破坏数组。
<?php
$ array = get_field('casts');
$ new_array = array();
foreach($ array as $ sub_array){
foreach($ sub_array as $ value){
//这个array_push()函数将$ value添加到$ new_array的末尾。
array_push($ new_array,$ value);
echo implode(,,$ new_array);
?>
implode()
下一次,添加标签 php
,否则我们的代码将不会获得颜色语法。 <?php
$array = get_field('casts');
$elements = $array;
foreach($array as $key => $value) {
echo implode(', ', $value)};
?>
$elements
are nested arrays.<?php
$array = get_field('casts');
$new_array = array();
foreach($array as $sub_array) {
foreach($sub_array as $value) {
array_push($new_array, $value);
}
}
echo implode(", ", $new_array);
?>
$elements = $array;
and then never using $elements
?foreach
) to implode an array.<?php
$array = get_field('casts');
$new_array = array();
foreach($array as $sub_array) {
foreach($sub_array as $value) {
// this array_push() function adds $value to the end of $new_array.
array_push($new_array, $value);
}
}
echo implode(", ", $new_array);
?>
implode()
php
, otherwise our codes won't get color syntax.