Home >Backend Development >PHP Tutorial >How Can I Convert a Comma-Separated String into a PHP Array?

How Can I Convert a Comma-Separated String into a PHP Array?

DDD
DDDOriginal
2024-12-30 14:18:10546browse

How Can I Convert a Comma-Separated String into a PHP Array?

Explode a Comma-Delimited String: Array Construction

Problem:

You need to parse a comma-separated string into an individual elements within an array.

Explanation:

The explode() function provides an effective way to split strings based on a given delimiter. In this case, commas will serve as the delimiter.

Solution:

<?php
$myString = "9,[email&#160;protected],8";
$myArray = explode(',', $myString);
print_r($myArray);
?>

Output:

Array
(
    [0] => 9
    [1] => [email&#160;protected]
    [2] => 8
)

Note that using explode() produces an array with each element representing a substring separated by the specified comma delimiter. In the provided input, the output array will contain three elements: '9', 'admin@example', and '8'.

The above is the detailed content of How Can I Convert a Comma-Separated String into a PHP 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