Home  >  Article  >  Backend Development  >  How to convert string to array in php

How to convert string to array in php

PHPz
PHPzOriginal
2023-04-19 10:08:44960browse

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);

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
)
  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);

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
)
  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"}';

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!

Statement:
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