Home Backend Development PHP Problem How to convert string to array in php

How to convert string to array in php

Apr 19, 2023 am 10:08 AM

PHP is a widely used web development language. It has many powerful features. For example, it allows us to convert a string into an array and perform various operations on the array. This article will teach you how to convert a string to an array.

  1. Use PHP's explode() function

PHP's built-in function explode() splits the string into an array. It requires two parameters, the first parameter is the character to split the string, and the second parameter is the string to be split.

For example, if you want to convert the string "apple,banana,orange" to an array, you can use the following code:

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

In the above code, we use comma as the delimiter to convert the string Split into arrays. Use the print_r() function to output the array. The output result is as follows:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)
Copy after login
  1. Use PHP’s str_split() function

str_split() function to split the string into single characters array. It takes one parameter, the string to be split.

For example, if you want to convert the string "hello" into an array, you can use the following code:

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

In the above code, we use the str_split() function to split the string into an array. Use the print_r() function to output the array. The output result is as follows:

Array
(
    [0] => h
    [1] => e
    [2] => l
    [3] => l
    [4] => o
)
Copy after login
  1. Convert the string to JSON format, and then use PHP's json_decode() function to convert it to an array.

If you have a string without delimiters and you know the string is in JSON format, you can convert it to an array.

For example, if you have a string in JSON format:

$str = '{"name": "John", "age": 30, "city": "New York"}';
Copy after login

You can use the json_decode() function to convert it to an array:

$arr = json_decode($str, true);
print_r($arr);
Copy after login

In the above code, We set the second parameter to "true" so that the json_decode() function will return an array instead of an object. Use the print_r() function to output an array. The output is as follows:

Array
(
    [name] => John
    [age] => 30
    [city] => New York
)
Copy after login

Summary

In PHP, there are many ways to convert a string into an array. Use the explode() function to split a string into an array based on the specified delimiter; use the str_split() function to split a string into an array of single characters. If you have a string in JSON format, you can use the json_decode() function to convert it to an array.

Since each method has different characteristics, you should choose the corresponding method according to your specific application scenario.

The above is the detailed content of How to convert string to array in 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
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 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
1672
14
PHP Tutorial
1277
29
C# Tutorial
1257
24