String conversion dimensional array php

WBOY
Release: 2023-05-22 19:52:06
Original
364 people have browsed it

In PHP programming, we often need to convert strings to arrays. Sometimes, we need to split a string into multiple elements according to specific delimiters and then form an array. In this article, we will explain how to convert a string to an array using PHP functions.

  1. explode function

PHP’s explode function is the most common way to split a string into multiple substrings and store them in an array. This function accepts two parameters: delimiter and source string. For example:

$str = "apple,banana,orange";
$arr = explode(",", $str);
print_r($arr);
Copy after login

Output result:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)
Copy after login

In the example, use comma as the separator. The explode function splits the string into three elements: apple, banana, and orange, and stores them into the array $arr.

  1. str_split function

PHP’s str_split function splits a string into individual characters and stores them into an array. This function accepts one parameter: the source string. For example:

$str = "hello";
$arr = str_split($str);
print_r($arr);
Copy after login

Output result:

Array
(
    [0] => h
    [1] => e
    [2] => l
    [3] => l
    [4] => o
)
Copy after login

In the example, the string hello is split into five elements: h, e, l, l and o, and they are stored in the array $arr.

  1. preg_split function

PHP’s preg_split function is similar to the explode function, but it uses regular expressions as delimiters. This function accepts two parameters: regular expression and source string. For example:

$str = "the quick brown fox jumps over the lazy dog";
$arr = preg_split("/s+/", $str);
print_r($arr);
Copy after login

Output result:

Array
(
    [0] => the
    [1] => quick
    [2] => brown
    [3] => fox
    [4] => jumps
    [5] => over
    [6] => the
    [7] => lazy
    [8] => dog
)
Copy after login

In the example, use regular expressions to split the string into multiple elements according to spaces and store them in the array $arr.

  1. sscanf function

PHP’s sscanf function extracts data from a string. This function accepts two parameters: source string and format string. The format string describes the format of the data to be extracted. For example:

$str = "John Doe,35";
sscanf($str, "%s,%d", $name, $age);
echo "Name: $name, Age: $age";
Copy after login

Output:

Name: John Doe, Age: 35
Copy after login

In the example, the format string specifies two data items: a string and an integer. The sscanf function extracts these data from the source string and stores them in the variables $name and $age respectively.

  1. json_decode function

PHP’s json_decode function parses a JSON-formatted string into an array or object. This function accepts one parameter: JSON string. For example:

$str = '{"name":"John", "age":35}';
$arr = json_decode($str, true);
print_r($arr);
Copy after login

Output result:

Array
(
    [name] => John
    [age] => 35
)
Copy after login

In the example, convert the JSON string to an array. The json_decode function parses it and stores it in the array $arr.

  1. unserialize function

PHP’s unserialize function converts a serialized string into a PHP variable or object. This function accepts one parameter: the serialized string. For example:

$str = 'a:2:{i:0;s:5:"apple";i:1;s:6:"orange";}';
$arr = unserialize($str);
print_r($arr);
Copy after login

Output result:

Array
(
    [0] => apple
    [1] => orange
)
Copy after login

In the example, convert the serialized string into an array. The unserialize function parses it and stores it in the array $arr.

Summary

PHP provides many functions to convert strings to arrays. It is important to choose the appropriate function according to your needs. Using these functions, we can quickly and easily convert strings to arrays to better complete programming tasks.

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

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!