Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.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 在数组中使用串行逗号_Php_Arrays_Function_Comma_Implode - Fatal编程技术网

Php 在数组中使用串行逗号

Php 在数组中使用串行逗号,php,arrays,function,comma,implode,Php,Arrays,Function,Comma,Implode,这里的新手第一次使用“爆炸”。我正在尝试处理串行逗号如何在数组中打印或不打印。我已经找到了几种有效的方法,我正试着根据我所看到的做我自己的旋转,但我的方法不起作用,我不知道为什么 我希望下面的函数会返回下面显示的结果,但在我测试它时,它似乎什么也没有返回。我看了一下我的格式、分号等,我被难住了。这有点超出我的理解,非常令人兴奋。我试图理解为什么这不起作用,以及如何使它在某个地方起作用,因为我的逻辑或理解是非常错误的,因为什么都没有发生 预期/希望的结果: A. a和b a、 b和c 实际结果:

这里的新手第一次使用“爆炸”。我正在尝试处理串行逗号如何在数组中打印或不打印。我已经找到了几种有效的方法,我正试着根据我所看到的做我自己的旋转,但我的方法不起作用,我不知道为什么

我希望下面的函数会返回下面显示的结果,但在我测试它时,它似乎什么也没有返回。我看了一下我的格式、分号等,我被难住了。这有点超出我的理解,非常令人兴奋。我试图理解为什么这不起作用,以及如何使它在某个地方起作用,因为我的逻辑或理解是非常错误的,因为什么都没有发生

预期/希望的结果: A. a和b a、 b和c

实际结果:

<?php
 $items = array('a','b','c');

 function render_array_as_serial_comma($items) {
 $items = $variables['items'];

 if (count($items) > 1) {
 $last = array_pop($items);
 return implode(', ', $items) . ' and ' . $last;
}
 return array_pop($items);

 render_array_as_serial_comma(array('a'));
 render_array_as_serial_comma(array('a', 'b'));
 render_array_as_serial_comma(array('a', 'b', 'c'));
 ?>

我想你忘了回声:)


也许一个
回音
会有帮助。。。。但是,如果数组中只有一个条目,
$variables['items']
,则$last将是一个问题-此变量在函数中不可用。我建议您查找类似的错误。谢谢-我将探索我在本地工作(在我的mac上使用MAMP),我认为已经打开了错误处理。但将其加载到我的站点时,我看到有一条错误消息,上面写着“警告:array_pop()希望参数1为array,在fileX.php的第12行中给出null,即“return array_pop($items);”。我现在还有很多事情要做。”很有可能我就是这么做的。引用荷马的话,哼!谢谢,它很有效。从中我学到了很多,包括如何在一个页面或整个站点上打开和关闭错误通知(我进行了一些学习)
<?php
$items = array('a', 'b', 'z');

function render_array_as_serial_comma($items) {
    $final_string = array_pop($items);
    if(count($items) >= 1) {
        $final_string = implode(', ', $items) . ' and ' . $final_string;
    }
    return $final_string;
}

echo render_array_as_serial_comma($items);