Home > Article > Backend Development > How to convert php string to array
php String to Array You can use the "explode()" function to split the string into an array. The operation method is: 1. Create a PHP sample file; 2. Define a character containing a comma delimiter. String variable "string"; 3. Use the "explode()" function to split the string according to commas and store the split substrings in the array "array"; 4. Use the "print_r()" function to print The contents of the array "$array" are enough.
Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.
In PHP, you can use the explode() function to split a string into an array. The explode() function accepts a delimiter and a string as parameters and returns an array consisting of the substrings after the string is split.
The following is a sample code that demonstrates how to convert a string to an array:
<?php $string = "apple,banana,orange"; array=explode(",",array = explode(",", array=explode(",",string); print_r($array); ?>
The output is:
Array ( [0] => apple [1] => banana [2] => orange )
In the above example, we define A string containing comma delimiters, then, we use the explode() function to split the string according to commas, and store the split substrings in the array array. Finally, we use the print_r() function Print the contents of array $array to verify the conversion result.
It should be noted that the explode() function will split the string according to the specified delimiter, and the elements in the returned array are the split substrings. In the example, we use comma , as the delimiter. You can modify the delimiter according to the actual situation to adapt to different string splitting needs.
The above is the detailed content of How to convert php string to array. For more information, please follow other related articles on the PHP Chinese website!