Thursday, September 4, 2025
HomeLanguagesHow to convert array to string in PHP ?

How to convert array to string in PHP ?

We have given an array and the task is to convert the array elements into string. In this article, we are using two methods to convert array to string.

Method 1: 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 same as that of join() function.

Syntax:

string implode($separator, $array)

Example:

PHP




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


Output:

Welcome to neveropen A Computer Science Portal

Method 2: 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"}}

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.

RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS