php IMPLODE 和 JOIN 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17914008/
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-08-25 16:40:03  来源:igfitidea点击:

What is the difference between IMPLODE & JOIN

php

提问by Khawer Zeshan

What is the difference between IMPLODE& JOINas both works the same way.

IMPLODE&之间有什么区别,JOIN因为两者的工作方式相同。

<?php
    $array = array(1,2,3);

    echo join(",", $array); //output 1,2,3
    echo implode(",", $array); //output 1,2,3
?>

Is there is any advantage of using one over another?

使用一种比另一种有什么优势吗?

回答by PlausibleSarge

They are aliases of each other. They should theoretically work exactly the same. Although, using explode/implode has been shown to increase the awesomeness of your code by 10%

他们是彼此的别名。它们理论上应该完全相同。虽然,使用爆炸/内爆已被证明可以将代码的精彩程度提高 10%

回答by Erman Belegu

Join: Join is an Alias of implode().

Join:Join 是 implode() 的别名。

Example:

例子:

<?php
$arr = array('Test1', 'Test2', 'Test3');
$str = join(",", $arr);
echo $str; 
?>

Output: Test1,Test2,Test3.

输出:Test1、Test2、Test3。

Implode: implode Returns a string from array elements.

Implode: implode 从数组元素返回一个字符串。

Example:

例子:

<?php
$arr = array('Test1', 'Test2', 'Test3');
$str = implode(",", $arr);
echo $str; 
?>

Output: Test1,Test2,Test3.

输出:Test1、Test2、Test3。

UPDATE:

更新:

I tested them in Benchmark and they are same in speed. There is no difference between them.

我在基准测试中对它们进行了测试,它们的速度相同。它们之间没有区别。

回答by Tom Auger

join()is an alias for implode(), so implodeis theoretically more "PHP native" though there is absolutely no performance increase to be gained by using it.

join()是 的别名implode(),因此implode理论上更“PHP 原生”,尽管使用它绝对不会提高性能。

On the other hand, join()is found in, amongst other languages, Perl, Python and ECMAScript (including JavaScript) and so is much more portable in terms of comprehensibility to a wider audience of programmers.

另一方面,join()在 Perl、Python 和 ECMAScript(包括 JavaScript)等其他语言中,它在更广泛的程序员受众的可理解性方面更具可移植性。

So although explodeand implodeare unarguably way more dramatic-sounding, I would vote for joinas a more universal way to express the concatenation of every value of an array into a string.

因此,尽管explodeimplode无疑听起来更加戏剧化,但我会投票支持join将数组的每个值串联成字符串的更通用的方式。

回答by vee

Can't think of any. The one is an alias of the other. They both combine array elements into a string.

一个都想不起来 一个是另一个的别名。它们都将数组元素组合成一个字符串。

回答by Asiamah Amos

In short the reality is that in php, join() and implode() methods function the same way.

简而言之,实际情况是在 php 中,join() 和 implode() 方法的功能相同。

One can be interchanged for the other as they work in the same way.

一个可以互换,因为它们以相同的方式工作。