laravel 如何将数组转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34132092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:46:54  来源:igfitidea点击:

How to convert array to string

phplaravel

提问by Adishri Kulkarni

I am new to PHP.

我是 PHP 新手。

I want to convert this array [["14785"],["125478"]] to string like this 14785,125478.

我想把这个数组 [["14785"],["125478"]] 转换成这样的字符串 14785,125478。

What is the way to do this?

有什么方法可以做到这一点?

回答by Niranjan N Raju

try this,

尝试这个,

Using implode()- Demo

使用implode()-演示

$array = ["14785"],["125478"]
$str = implode(',',$array);
echo $str;

Using join()- Demo

使用join()-演示

$array =  ["14785","125478"];
$str = join(",",$array);
echo $str;

EDIT

编辑

For multi-dimention array, - Demo

对于多维数组, - Demo

$arr = [["14785"],["125478"]];
$str = implode(',', array_map(function($el){ return $el[0]; }, $arr));

echo $str;

回答by Milan Maharjan

use php's implodefunction

使用php的implode功能

$arr =  ["14785","125478"];
echo implode(",",$arr);

回答by NimeSh Patel

Using implode()

使用内爆()

 <?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>