Home > Backend Development > PHP Problem > How to convert php array string into array

How to convert php array string into array

PHPz
Release: 2023-04-23 16:30:50
Original
574 people have browsed it

Converting a string to an array or an array to a string is a very common task in PHP. Conversion between strings and arrays is especially important when we need to process data obtained from a database or other sources. So, this article will explain how to convert string to array and array to string in PHP.

How to convert a string into an array

In PHP, you can use the explode() function to split a string into an array. The explode() function requires two parameters. The first parameter is the delimiter, which can be any character or string, used to specify where to split the string into array elements. The second parameter is the string to be split.

The following is an example of using the explode() function to convert a string into an array:

$string = "apple,banana,orange";
$array = explode(",", $string);
print_r($array);
Copy after login

Output result:

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

In this example, we use commas to convert the string into an array Split into arrays. Therefore, we pass comma as the first parameter and the string apple, banana, orange as the second parameter in the explode() function. We then store the results in the $array array and print the array using the print_r() function.

If you are not sure which delimiter to use, choose a character or string that uniquely identifies the array elements. By default, the explode() function will split the string into chunks of the same length, so different delimiters can be used to specify different delimiters.

How to convert an array to a string

In PHP, you can use the implode() function to convert an array to a string. The implode() function requires two parameters, the first parameter is the delimiter to be used, and the second parameter is the array to be connected.

The following is an example of converting an array to a string using the implode() function:

$array = array("apple", "banana", "orange");
$string = implode(",", $array);
echo $string;
Copy after login

Output result:

apple,banana,orange
Copy after login

In this example, we use comma as the delimiter Concatenate arrays into a string. Therefore, we pass comma as the first parameter and the array("apple", "banana", "orange") as the second parameter in the implode() function. We then store the result in $string and print the string using echo statement.

Same as using the explode() function, you can also use any character or string as the delimiter. You just need to make sure you specify the correct delimiter with the first parameter of the implode() function.

Conclusion

Converting string to array and converting array to string are common tasks in PHP. You can easily perform these conversions using the explode() and implode() functions. No matter what delimiter you use, just make sure you use the correct arguments to execute these functions correctly. Hopefully this article will help you get better at handling arrays and strings in PHP programming.

The above is the detailed content of How to convert php array string into array. 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