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

How to convert php string to array

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2023-08-01 15:16:06628browse

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.

How to convert php string to array

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!

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