How To Convert Array Into String In PHP?

PHP arrays are used to store different types of multiple dates into a single variable. PHP arrays can also store multiple string type items. In some cases, we may want to convert multiple string array items into a single string. PHP programming language provides different methods to convert an array into a string.

Convert Array Into String In PHP Using implode() Function

implode() function is the most appropriate function to convert arrays items into a string. implode() function has the following syntax where you can specify a glue or separator for the converted string.

string implode( string $glue, arrays $arr)
  • $glue is the separator for the created string. It is optional where it not specified an empty string will be used.
  • $arr is the array where the items will be extracted.
  • Return type of the implode function will be string as expected.
<?php

   $arr= array('ahmet', 'ali', 'baydan');

   $comma_separated = implode(",", $arr);

   $space_separated = implode(" ", $arr);

   $doublecolon_separated = implode(":", $arr);

   $plus_separated = implode("+", $arr);

   $dash_separated = implode("-", $arr);

   $dash_separated = implode("X", $arr);

?>

Convert Array Into String In PHP Using json_encode() Function

JSON is a popular data format used in web applications to transmit different types of data. JSON data format is similar to the PHP array where json_encode() function can be used to convert PHP array into a string. json_encode() has a simple function where the PHP array is provided as a parameter.

<?php

$arr= array('ahmet', 'ali', 'baydan');

$myJSON = json_encode($arr);

echo $myJSON;

?>

Convert Array Into String In PHP Using foreach Loop

Even there are different functions provided by the PHP we can also create our own solution to convert the array into a string. The most appropriate way is using the foreach loop where it will iterate over all array items and convert them into a string.

<?php

$arr= array('ahmet', 'ali', 'baydan');

$mystr="";

foreach( $arr as $value){
   $mystr = $mystr + $value;
}

?>

Convert Array Into String In PHP Using print_r() Function

print_r() is a function used to print an array to the standard output. We can use print_r() function in order to print an array into the string. But keep in mind that this function will also print index numbers with array syntax.

<?php

$arr= array('ahmet', 'ali', 'baydan');

$str = print_r( $arr , false );

?>

Convert Array Into String In PHP Using var_dump() Function

var_dump() function is similar to the print_r() function but with a little difference. var_dump() function also prints the type of the item. We will provide the array into the var_dump() function like below. Also the var_dump() function output the string content directly to the standard output not into a variable.

<?php

$arr= array('ahmet', 'ali', 'baydan');

var_dump($arr);

?>

Convert Array Into String In PHP Using join() Function

join() is a function that is used to join or concatenate multiple items into a single string. Array items can be joined together with the join() function and we will also provide a separator as a first parameter to the join() function.

join(string $separator, array $arr);
  • $separator is the separator characters that will be put between every item in a string.
  • $arr is the array or enumerable type in which items will be converted into a string and joined into a single string.
<?php

$arr= array( 'ahmet', 'ali', 'baydan');

$mystr = join( ',' , $arr );

?>

Leave a Comment