PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
No Result
View All Result
Home How-to

How to convert from string to array in PHP

A handy guide to convert from string to array using PHP.

March 26, 2022
Reading Time: 2 mins read
PHP101.net - How to convert from string to array in PHP

PHP101.net - How to convert from string to array in PHP

Share on FacebookShare on Twitter

Assuming we are given a comma-separated fruit list in string format:

$fruits = "orange, banana, apple";

Now, we want to convert the list into a PHP array to further processing it. That is what this short how-to article is about. We will guide you on how to convert from string to array in PHP.

We also had another detailed guide written on multiple methods to do the opposite – convert from array to string in PHP.

  • How to convert from array to string in PHP
    Short guide on converting from array to string in PHP easily that works.

Quick solution

Here are the quick solution for the above question:

$fruitsArray = explode( ',', $fruits );

This line will:

  • Take the comma , as the separator
  • Separate the string into a $fruitsArray array with 03 elements, orange, banana, apple.

To know more details on how it works, here are the detailed guide.

To convert from string to array in PHP

Converting from string to array in PHP, or in another meaning, is to separate the string into parts and join these parts into an array using boundaries formed by a separator.

The simplest way to do that is by the built-in explode function.

Using explode function

We can easily use the PHP explode function to separate a string from PHP and convert it into array. The function syntax is:

explode(string $separator, string $string, int $limit = PHP_INT_MAX)

In this function:

  • $separator is the separator for the function to split the string into parts.
  • $string is the string to be converted
  • (Optional) $limit is the maximum of elements to be split and converted into array from the string. By default, its value equals PHP_INT_MAX which is the largest integer supported by your current PHP build.
    If you use $limit, the last element of the converted array will contain the rest of the string that is not converted.

The return of the explode function will be the converted array.

Examples

For the beginning question on the $fruits fruit list, we will use the comma (,) as the function separator, because it is also the main separator in the string.

// Convert $fruits list into array
$fruitsArray = explode( ',', $fruits );

We will get the result as an array with the expected elements.

print_r($fruitsArray);

// Output
Array
(
    [0] => Orange
    [1] => Banana
    [2] => Apple
)

Final thoughts

The explode function is very powerful and easy to use to convert string to array in PHP with a separator.

You might also like

  • How to convert from array to string in PHP

References

  • PHP.net Official Documentation on explode function
Tags: convert string to arrayphp convert string to arrayphp string to arraystring to array
Previous Post

How to set MySQL charset (or MariaDB) by PHP

Next Post

How to get user IP address using PHP

Related Posts

PHP101.net - How to set timezone with PHP

How to set timezone with PHP easily (multiple methods)

March 9, 2023
PHP101.net - How to redirect to another page using PHP

How to redirect to another page using PHP

March 8, 2023

Table of Contents

    • Quick solution
    • To convert from string to array in PHP
    • Using explode function
    • Examples
    • Final thoughts
    • You might also like
    • References
Next Post
PHP101.net - How to get user IP address using PHP

How to get user IP address using PHP

We deliver free and helpful resources for learning
and using PHP programming language.

About
Contact Us
Privacy Policy
Main Sections

Home
Deploy
How-to
Tutorial
You might be interested in...

How to Write PHP Comments (and best practices)

Deploy a webserver with XAMPP on Windows for PHP development

How to download file from URL using PHP

How to install Composer on Linux

Deploy a webserver with Laragon on Windows for PHP development

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone.

No Result
View All Result
  • Home
  • Deploy
  • How-to
  • Tutorial

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone. | Privacy Policy

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.