Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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 用逗号分隔$item_Php - Fatal编程技术网

Php 用逗号分隔$item

Php 用逗号分隔$item,php,Php,我想要像这样的昏迷输出 Rubber Ducky™ Gold fish & Tank David Byrne and My Fair Lady - MP3 file download (0 MB) PHP是如何实现的? Rubber Ducky™, Gold fish & Tank, David Byrne, My Fair Lady MP3 file download (0 MB) 这将在项目名称之间打印一个“,” 或者,您也可以这样做: <?php $first

我想要像这样的昏迷输出

Rubber Ducky™ Gold fish & Tank David Byrne and My Fair Lady - MP3 file download (0 MB)
PHP是如何实现的?


Rubber Ducky™, Gold fish & Tank, David Byrne, My Fair Lady MP3 file download (0 MB)
这将在项目名称之间打印一个“,”

或者,您也可以这样做:

<?php
$first = true;
foreach ($invoice->cart->items as $id => $item) {
    if ($first) {
        $first = false;
    } else {
        echo ', ';
    }
    echo $item->getName();
}
?>

使用函数。差不多

<?=implode(', ', array_map(function($item) { return $item->getName(); }, $invoice->cart->items))?>
<?=implode(', ', array_map(function($item) { return $item->getName(); }, $invoice->cart->items))?>
 $array = $invoice->cart->items;
 echo implode(',',$array);
$i = 0;
foreach( $invoice->cart->items as $item ) {
    echo ($i?', ':'').$item->getName();
    ++$i;
}