How to convert arrays and strings in PHP (must read)

醉折花枝作酒筹
Release: 2023-03-11 17:48:01
forward
4445 people have browsed it

In PHP, arrays and strings can be converted. Today we will introduce the method of converting arrays and strings. Friends in need can refer to it.

How to convert arrays and strings in PHP (must read)

1. Convert array to string

implode

Method introduction:

function implode ($glue = "", array $pieces) {}
Copy after login

Mainly has two parameters

One is the connector (glue: the meaning of glue), the default is an empty string

The other is the array

Use

$test = array("hello","world","php"); echo implode("-",$test);
Copy after login

Result:

hello-world-php
Copy after login
Copy after login

What if it is an array in the form of K-V?

$test = array("h"=>"hello","w"=>"world","p"=>"php"); echo implode("-",$test);
Copy after login

The result is still

hello-world-php
Copy after login
Copy after login

, indicating that it still only affects value.

2. Split the string into an array

2.1 Split by a certain character

function explode ($delimiter, $string, $limit = null) {}
Copy after login

explode (explode, maybe explode the string?)

Parameter explanation:

delimiter, delimiter

limit

Document google translation results:

If limit is set to positive, the returned array will Contains the maximum limiting element, with the last element containing the remainder of the string.

Personal understanding:

limit limits the number of split parts. The last part is the remaining string after splitting. Of course, if it is 1, it is the string itself (has passed Experiment)

Code demonstration:

$str="hello-world-php"; $result = explode("-", $str); var_dump($result); $result = explode("-", $str,2); var_dump($result);
Copy after login

Output result:

array(3) { [0]=> string(5) "hello" [1]=> string(5) "world" [2]=> string(3) "php" } array(2) { [0]=> string(5) "hello" [1]=> string(9) "world-php" }
Copy after login

2.2 Reading by distance

Maybe the string does not need to be divided, but each one needs to be Take out the characters, that is, read them out one by one

Original document:

** * Convert a string to an array * @link http://php.net/manual/en/function.str-split.php * @param string $string 

* The input string. *

* @param int $split_length [optional]

* Maximum length of the chunk. *

* @return array If the optional split_length parameter is * specified, the returned array will be broken down into chunks with each * being split_length in length, otherwise each chunk * will be one character in length. *

*

* false is returned if split_length is less than 1. * If the split_length length exceeds the length of * string, the entire string is returned as the first * (and only) array element. * @since 5.0 */ function str_split ($string, $split_length = 1) {}

Copy after login

Partial translation:

Array If the optional split_length parameter is specified, the returned array will is split into chunks of length split_length, otherwise each chunk would be one character long.

Convert a string to an array, isn’t this just converting a string into an array, there is nothing to say about the usage

Now I want to give it a try, if it is 5 characters in length The string is read in two digits. If there are less than two digits, should the last digit be retained?

Of course, presumably for the sake of data integrity, it should be retained.

$str = "hello"; var_dump(str_split($str,2));
Copy after login

The result is just as I expected

array(3) { [0]=> string(2) "he" [1]=> string(2) "ll" [2]=> string(1) "o" }
Copy after login

Recommended learning:php video tutorial

The above is the detailed content of How to convert arrays and strings in PHP (must read). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!