Home > Backend Development > PHP Problem > php array to string

php array to string

藏色散人
Release: 2023-02-24 07:34:02
Original
4835 people have browsed it

php array to string

#PHP array to string, PHP turns the array into a string. Today I will introduce to you a PHP function implode(). It can be used to convert PHP arrays to strings. Let’s learn together:

PHP arrays to strings

Related examples

Convert arrays The elements are combined into a string:

<?php
    $array = array(&#39;Hello&#39;,&#39;World!&#39;,&#39;I&#39;,&#39;love&#39;,&#39;Beijing!&#39;);
    echo implode(" ",$array);
?>
Copy after login

implode() function

Definition and usage

implode() function returns by A string composed of array elements.

Note: The implode() function accepts two parameter orders. However, due to historical reasons, explode() does not work. You must ensure that the separator parameter comes before the string parameter.

Note: The separator parameter of the implode() function is optional. However, for backward compatibility, it is recommended that you use two parameters.

Note: This function is binary safe.

Syntax

implode(separator,array)
Copy after login

Parameter Description

separator is optional. Specifies what is placed between array elements. Default is "" (empty string).

array required. Arrays to be combined into strings.

Note: The implode() function accepts two parameter orders. However, due to historical reasons, explode() does not work. You must ensure that the separator parameter comes before the string parameter.

Example 1

Separate array elements with different characters:

<?php
    $arr = array(&#39;Hello&#39;,&#39;World!&#39;,&#39;I&#39;,&#39;love&#39;,&#39;Shanghai!&#39;);
    echo implode(" ",$arr)."<br>";
    echo implode("+",$arr)."<br>";
    echo implode("-",$arr)."<br>";
    echo implode("X",$arr);
?>
Copy after login

Related recommendations: "PHP Tutorial"

The above is the detailed content of php array to string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template