search
HomeBackend DevelopmentPHP ProblemTeach you how to convert arrays and strings into each other in PHP in five minutes

In the previous article "How to handle the type of PHP array" we introduced the array types in PHP in detail. In this article we will take a look at how to implement arrays and strings in PHP. I hope it will be helpful to everyone with relevant knowledge about mutual conversion!

Teach you how to convert arrays and strings into each other in PHP in five minutes

The two most commonly used variable types in PHP are arrays and strings. During the development process, you will inevitably encounter the problem of how to convert strings into arrays. situation, or the situation of converting an array into a string.

When encountering such a situation, how should we solve it? These two variable types are so common and commonly used. There are two functions in PHP that can perform interaction between strings and arrays. Convert. That is, the explode() function can convert a string into an array, and the implode() function can convert an array into a string.

Then let’s learn the use of these two functions together, and how to convert strings and arrays to each other.

<strong><span style="font-size: 20px;">explode()</span></strong>Function - Convert string to array

explode()The function can use a string to split another string and then return the result is an array. The syntax format of the explode() function is as follows:

explode($delimiter, $string [, $limit])

What needs to be noted is:

The key point to understand this function is the delimiter, which is the parameter $delimiter, which represents the delimiter character used to separate strings. It is used to mark what is used to become an independent array element. The parameter $string represents the string used to separate, and the parameter $limit is an optional parameter and can be empty.

Next let’s take a look at the usage of the explode() function through an example. The example is as follows:

<?php
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0] . &#39;<br/>&#39;; // piece1
echo $pieces[1] . &#39;<br/>&#39;; // piece2
print_r($pieces);
?>

Output result:

Teach you how to convert arrays and strings into each other in PHP in five minutes

Through the above example, we can find that the explode() function converts a string into an array, and the function parameter $limit is empty in the above example.

It should be noted that when the parameter $limit is empty, the function returns all array elements; when the parameter $limit is When the number is positive, the maximum number of elements in the returned array is the number filled in $limit; when the parameter $limit is a negative number, all elements except the last $limit elements are returned. Element; if parameter $limit is 0, it is treated as 1.

The examples are as follows:

<?php
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0] . &#39;<br/>&#39;; // piece1
echo $pieces[1] . &#39;<br/>&#39;; // piece2
print_r($pieces);
echo &#39;<br/>&#39;;
$pieces = explode(" ", $pizza , 2);
print_r($pieces);
echo &#39;<br/>&#39;;
$pieces = explode(" ", $pizza , 0);
print_r($pieces);
echo &#39;<br/>&#39;;
$pieces = explode(" ", $pizza , -2);
print_r($pieces);
?>

Output results:

Teach you how to convert arrays and strings into each other in PHP in five minutes

The above examples are the results returned by different $limit parameters Different, but the conversion of string to array is completed through the explode() function. Next we look at how to convert an array into a string.

<strong><span style="font-size: 20px;">implode()</span></strong>-Convert array to string

In PHP, a one-dimensional array can be converted into a string through the implode() function. The syntax format of this function is as follows:

implode($glue, $array)

What needs to be noted is: $glue can be regarded as a glue symbol. Its function is to connect each element in the array together. By default, the parameter $glue is an empty string. Parameter $array represents the string that needs to be converted.

Next, let’s take a look at the use of the implode() function through an example. The example is as follows:

<?php
    $arr = [&#39;元素1&#39;,&#39;元素2&#39;,&#39;元素3&#39;,&#39;元素4&#39;,&#39;元素5&#39;];
    $str = implode($arr);
    echo $str.&#39;<br>&#39;;
?>

Output result:

Teach you how to convert arrays and strings into each other in PHP in five minutes

In the above example, the parameter $glue is set to empty, which means that this parameter does not need to be written, but if it is written, the glue character will be inserted into the middle of each array element to convert it into a string.

The example is as follows:

<?php
    $arr = [&#39;元素1&#39;,&#39;元素2&#39;,&#39;元素3&#39;,&#39;元素4&#39;,&#39;元素5&#39;];
    $str = implode($arr);
    echo $str.&#39;<br>&#39;;
    $str = implode(&#39;,&#39; , $arr);
    echo $str.&#39;<br>&#39;;
    $str = implode(&#39;---&#39; , $arr);
    echo $str.&#39;<br>&#39;;
    $str = implode(&#39;/&#39; , $arr);
    echo $str
?>

Output result:

Teach you how to convert arrays and strings into each other in PHP in five minutes

In the above example, it is done through different parameters $glue Array to string conversion.

Mutual conversion between strings and arrays

Let’s take a look at the mutual conversion between arrays and strings through examples. The examples are as follows :

<?php
    $arr = [&#39;元素1&#39;,&#39;元素2&#39;,&#39;元素3&#39;,&#39;元素4&#39;,&#39;元素5&#39;];
    $str = implode($arr);
    echo $str.&#39;<br>&#39;;
    $arr1 = explode(" ", $str);
    print_r($arr1);
    echo &#39;<br/>&#39;;
    $str1 = implode($arr1);
    echo $str1.&#39;<br>&#39;;
?>

Output result:

Teach you how to convert arrays and strings into each other in PHP in five minutes

From the above example, we understand that arrays and arrays can be completed through the implode() function and explode() function. Convert between strings.

If you are interested, you can click on "PHP Video Tutorial" to learn more about PHP knowledge.

The above is the detailed content of Teach you how to convert arrays and strings into each other in PHP in five minutes. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software