Open In App

How to Convert Array to String in PHP?

Last Updated : 16 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

We are given an array and the task is to convert the array elements into a string.

Below are the approaches to convert an array to a string in PHP:

Using implode() function

The implode() method is an inbuilt function in PHP and is used to join the elements of an array. The implode() method is an alias for PHP | join() function and works exactly the same as that of the join() function.

Syntax:

string implode($separator, $array)

Example:

PHP
<?php  

// Declare an array 
$arr = array("Welcome","to", "GeeksforGeeks", 
    "A", "Computer","Science","Portal");  
  
// Converting array elements into
// strings using implode function
echo implode(" ",$arr);
 
?>

Output:

Welcome to GeeksforGeeks A Computer Science Portal

Using json_encode() Function

The json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation.

Syntax:

string json_encode( $value, $option, $depth )

Example:

PHP
<?php 

// Declare multi-dimensional array 
$value = array( 
    "name"=>"GFG", 
    array( 
        "email"=>"abc@gfg.com", 
        "mobile"=>"XXXXXXXXXX"
    ) 
); 

// Use json_encode() function 
$json = json_encode($value); 

// Display the output 
echo($json); 

?> 

Output:

{"name":"GFG","0":{"email":"abc@gfg.com","mobile":"XXXXXXXXXX"}}

Using sprintf

This PHP code uses sprintf() to format an array into a string. The %s placeholders in the format string match each array element, expanding with …$array, resulting in “Hello World in PHP” as output.

Example:

PHP
<?php
$array = array('Hello', 'World', 'in', 'PHP');
$string = sprintf('%s %s %s %s', ...$array);

echo $string;  // Output: Hello World in PHP
?>

Output
Hello World in PHP

Using serialize() Function

The serialize() function creates a serialized string representation of a PHP value, including arrays. This can be useful for storing arrays in a text format that can be easily restored.

Example: In this example, the convertArrayToString function uses the serialize() function to convert the input array into a serialized string. This string can be stored or transmitted as needed.

PHP
<?php
function convertArrayToString($array) {
    return serialize($array);
}

// Example usage
$array = ['name' => 'GFG', 'email' => 'abc@gfg.com', 'mobile' => 'XXXXXXXXXX'];
$string = convertArrayToString($array);
echo $string;
// Output: a:3:{s:4:"name";s:3:"GFG";s:5:"email";s:12:"abc@gfg.com";s:6:"mobile";s:10:"XXXXXXXXXX";}

// To unserialize back to array
$unserializedArray = unserialize($string);
print_r($unserializedArray);


?>

Output
a:3:{s:4:"name";s:3:"GFG";s:5:"email";s:11:"abc@gfg.com";s:6:"mobile";s:10:"XXXXXXXXXX";}Array
(
    [name] => GFG
    [email] => abc@gfg.com
    [mobile] => XXXXXXXXXX
)

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.



Next Article

Similar Reads

three90RightbarBannerImg