Home > Backend Development > PHP Problem > How to convert array type in php

How to convert array type in php

PHPz
Release: 2023-04-23 09:58:00
Original
622 people have browsed it

In PHP, converting a string to an array is a common type conversion method. This is typically used when retrieving data from a database or getting data from an external API. In this article, we'll look at how to convert a PHP string to an array, including several tips and best practices.

Method 1: Use the explode function

The explode function is a function that splits a string into an array. It accepts two parameters: the delimiter and the string to split. Here is an example of using the explode function to convert a string to an array:

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

This will output the following:

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

In this example, we are using commas as delimiters. Pass the $str string to the explode function to convert it into an array. This function returns an array where each element is part of a string split by the delimiter.

Method 2: Use preg_split function

The preg_split function is a powerful function in PHP that can use regular expressions to split strings. The following is an example of using the preg_split function to convert a string to an array:

$str = "apple|banana|orange";
$arr = preg_split("/\|/", $str);
print_r($arr);
Copy after login

This will output the following:

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

In this example, we use vertical bars as separators. By passing a regular expression to the preg_split function, we can let the function use it for splitting. By default, the preg_split function will automatically split the string according to spaces.

Method 3: Use the json_decode function

If your string is a valid JSON object, you can convert it to a PHP array, using the json_decode function. The following is an example of using the json_decode function to convert a string to an array:

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

This will output the following:

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

In this example, we first pass a JSON string to the json_decode function. Then set a second argument of type boolean to true, which tells the function to convert to an associative array. If you leave this parameter empty or set to false, an object will be returned.

It should be noted that this method can only convert arrays generated from JSON strings.

Conclusion

Here are three methods of converting strings to arrays in PHP. Use the explode function or preg_split function to split the string, and use the json_decode function to convert the JSON string into an array. These methods all make it easy to convert your strings to array types so you can easily handle them in your code.

The above is the detailed content of How to convert array type in 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