how to convert array into string in php code example
Example 1: Array to String Conversion in PHP
$gadget = array( 'computer', 'mobile', 'tablet' );
echo implode($arr);
Example 2: php Array to string conversion
Using implode() function in Php
-----------------------
Syntax
implode(separator,array);
Example
<?php
$dummyArr = array("Hello","Greppers,","Ankur","here !");
echo implode(" ",$dummyArr);
?>
Output:
Hello Greppers, Ankur here !
Example 3: array to string conversion in php
$person = [
'name' => 'Jon',
'age' => 26,
'status' => null,
'friends' => ['Matt', 'Kaci', 'Jess']
];
echo json_encode($person);
Example 4: Convert an Array to a String in PHP
phpCopyjson_encode( $ArrayName );
Example 5: array to string conversion in php
function subArraysToString($ar, $sep = ', ') {
$str = '';
foreach ($ar as $val) {
$str .= implode($sep, $val);
$str .= $sep;
}
$str = rtrim($str, $sep);
return $str;
}
echo subArraysToString($food);
Example 6: Convert an Array to a String in PHP
phpCopyimplode($string, $arrayName);