Home Backend Development PHP Problem String conversion dimensional array php

String conversion dimensional array php

May 22, 2023 pm 07:52 PM

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!

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

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24