Asked 3 years ago
30 Jul 2021
Views 773
Rebecca

Rebecca posted

Array to string in php

Array to string in php
fatso

fatso
answered Sep 25 '21 00:00

implode() function can join all all elements of the array by given string and return a string .


Syntax :
implode(glue,array)

implode() have the first argument is a string by which all elements of the array are joined together to make a single string.


For example :

$a=array("i","am","coder");
$b=implode(" ",$a);
echo $b; 

all array elements are joined with " "(space) by implode() function in above example
Melyna

Melyna
answered Aug 22 '21 17:24

implode() function can convert the array to string in PHP.


$array=array("array",,"is""string","now");
echo implode(" ",$array); // it print array is string now



implode("first argument is glue","second argument must be
array ")


so in above we glued the array value with one space and make a string in returns


join() function is can convert the array to string in PHP.


$a=array("a",,"b""c","d");
echo join(" ",$array); 


join("first argument is glue","second argument must be array ")

so in above we glued the array value with one space and make a string in returns with join() function in PHP

join() function is alias of the implode() function in PHP


if you want to convert the array to JSON string.

json_encode() function will encode array to JSON, JSON is one type of string which formatted so it can convert JSON string to array again.

$response=array("stauts"=>"1");
echo json_encode($response);


The JSON format is used to information exchange between server to server or server to client
Server Ajax response will be sent back to the browser in the format of JSON
Post Answer