An array is a data type that allows storing multiple related values under a single variable. Arrays are incredibly useful when you need to manage associated data without the risk of repetition.

This guide will teach you how to convert an array of values to a comma-separated string.

The Basics – An Array

Let us start by understanding a PHP array and how we can use it in our programs. Feel free to skip this section if you are versed in how PHP arrays work.

In simple terms, an array is a variable that can hold multiple values of the same type. A typical use case of an Array is to store related information. For example, you can store information related to a user in an array.

We use the array() function to create an array in PHP. Consider the example below that illustrates creating a simple Array in PHP.

The code above creates a simple array showing information related to a specific user.

There are various arrays in PHP, such as associative, multidimensional arrays. We will not discuss them in this tutorial as they are out of the scope of this guide. Check the documentation and other resources to learn more.

Introduction to PHP Implode Function

We will use the implode function to convert an array to a string of comma-separated values. This function allows you to take an array and convert it to a string where a specified delimiter separates individual values.

The function’s syntax is as shown:

It takes a separator and an array as the arguments.

The function returns a string of values separated by the set delimiter.

Convert Array to Comma Separated String

To convert an array of items to a string, we can use the implode function and pass the array and a comma as the delimiter.

Consider the example shown below:

<?php

    $databases = array(“MySQL”, “Redis”, “MongoDB”, “PostgreSQL”, “SQLite”);

    print_r($databases);

    $imploded_string = implode(“,”, $databases);

    print_r($imploded_string);

?>

In the example code above, we create an array that holds various databases. Next, we use the implode function to convert the collection to a comma-separated string.

Once you run the code, you should see the output as shown:

$ php implode.php

[OUTPUT]

(

    [0] =>MySQL

    [1] =>Redis

    [2] =>MongoDB

    [3] =>PostgreSQL

    [4] =>SQLite

)

MySQL,Redis,MongoDB,PostgreSQL,SQLite

Example 2

The following example shows how to pass a comma and space as the delimiter.

<?php

    $databases = array(“MySQL”, “Redis”, “MongoDB”, “PostgreSQL”, “SQLite”);

    print_r($databases);

    $imploded_string = implode(“, “, $databases);

    print_r($imploded_string);

?>

The code above should return the elements in the array separated by a comma and space.

(

    [0] =>MySQL

    [1] =>Redis

    [2] =>MongoDB

    [3] =>PostgreSQL

    [4] =>SQLite

)

MySQL, Redis, MongoDB, PostgreSQL, SQLite

Example 3

Does the function work on a multidimensional array? Simple answer, no. The function cannot implode multidimensional array as shown:

<?php

    $databases = array(

        “MySQL” =>array(

            “host” =>“localhost”,

            “port” =>“3306”

        ),

        “Redis” =>array(

            “host” =>“localhost”,

            “port” =>“6379”

        )

        );

    $imploded_string = implode(“, “, $databases);

    print_r($imploded_string);

?>

PHP will return “Array to string conversion” if you run the code above.

Closing

This tutorial shows you how to use the PHP implode function to create a string of comma-separated values. The implode function has an alias of join(), which you can perform the same task.

Thank you for reading!

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/john-150×150.png61c81f28796d4.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list