Current Location: Home> Latest Articles> How to Convert a PHP Multidimensional Array to a String

How to Convert a PHP Multidimensional Array to a String

gitbox 2025-06-27

What Is a PHP Multidimensional Array?

Before learning how to convert a PHP multidimensional array to a string, it’s important to understand what a multidimensional array is. In PHP, a multidimensional array refers to an array that contains one or more arrays as its elements—essentially an array of arrays. This structure is useful for representing complex datasets.

Here’s a basic example of a multidimensional array:

$array = array(
    array("name" => "Tom", "age" => 20),
    array("name" => "Lucy", "age" => 18)
);

Each sub-array contains key-value pairs for "name" and "age", making this a two-dimensional array.

How to Convert a PHP Multidimensional Array to a String

Using foreach Loop to Concatenate Elements

One of the most straightforward ways to convert a multidimensional array into a string is by using a foreach loop to iterate through each sub-array. You can then use implode() to turn each sub-array into a comma-separated string and concatenate the results.

<php
$array = array(
    array("name" => "Tom", "age" => 20),
    array("name" => "Lucy", "age" => 18)
);

$str = '';
foreach ($array as $item) {
    $str .= implode(',', $item) . '|';
}
$str = substr($str, 0, -1);
echo $str; // Output: "Tom,20|Lucy,18"
?>

In this example, each inner array is converted to a string using implode(), and then joined together with a pipe "|" character.

Using a Recursive Function for Deeply Nested Arrays

For arrays with deeper nesting, a recursive function is more flexible. The function can process each element, determine if it's an array, and recursively handle it if needed.

<php
$array = [
    [
        'name' => 'Tom',
        'age' => 20,
        'score' => [
            'math' => 80,
            'eng' => 90
        ]
    ],
    [
        'name' => 'Lucy',
        'age' => 18,
        'score' => [
            'math' => 70,
            'eng' => 95
        ]
    ]
];

function arrayToString($array)
{
    if (!is_array($array)) {
        return $array;
    }
    $str = '';
    foreach ($array as $key => $value) {
        $str .= $key . ':' . arrayToString($value) . ',';
    }
    $str = rtrim($str, ',');
    return $str;
}

$ava_str = arrayToString($array);
echo $ava_str; // Output: "name:Tom,age:20,score:math:80,eng:90,name:Lucy,age:18,score:math:70,eng:95"
?>

This function checks whether each value is itself an array. If so, it recursively processes it; otherwise, it adds the key-value pair to the resulting string. The rtrim() function removes any trailing commas.

Conclusion

This article introduced the concept of multidimensional arrays in PHP and demonstrated two effective methods for converting them to strings. The foreach loop method is suitable for simple arrays, while the recursive function is ideal for deeply nested structures. Choose the approach that best fits your specific use case and data complexity.