How to convert string to array in php
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.
- 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);
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 )
- 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);
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 )
- 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"}';
You can use the json_decode() function to convert it to an array:
$arr = json_decode($str, true); print_r($arr);
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 )
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics









