xxxxxxxxxx
<?php
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated;
// lastname,email,phone
?>
xxxxxxxxxx
$gadget = array( 'computer', 'mobile', 'tablet' );
echo implode($arr);
xxxxxxxxxx
// Use json_encode to collapse the array to json string:
$stuff = array(1,2,3);
print json_encode($stuff); //Prints [1,2,3]
xxxxxxxxxx
phpCopy<?php
$array = ["Lili", "Rose", "Jasmine", "Daisy"];
$JsonObject = json_encode($array);
echo "The array is converted to the Json string.";
echo "\n";
echo"The Json string is $JsonObject";
?>
xxxxxxxxxx
// for one-dimentional arrays
$str = implode('|', $arr); // "v1|v2|v3"...
// for multi-dimensional/structured arrays, or to keep hierarchy
$str = json_encode($arr);
// or
$str = var_export($arr);
xxxxxxxxxx
$person = [
'name' => 'Jon',
'age' => 26,
'status' => null,
'friends' => ['Matt', 'Kaci', 'Jess']
];
echo json_encode($person);
// {"name":"Jon","age":26,"status":null,"friends":["Matt","Kaci","Jess"]}
xxxxxxxxxx
phpCopy<?php
$array = ["Lili", "Rose", "Jasmine", "Daisy"];
$JsonObject = serialize($array);
echo "The array is converted to the Json string.";
echo "\n";
echo"The Json string is $JsonObject";
?>
xxxxxxxxxx
/ 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);
?>
xxxxxxxxxx
phpCopy<?php
$arr = array("This","is", "an", "array");
$string = implode(" ",$arr);
echo "The array is converted to the string.";
echo "\n";
echo "The string is '$string'";
?>
xxxxxxxxxx
<?php
$array = array("I", "Like", "Coffe");
// implode($separator, $array) : string
echo implode(" ", $array);
//>> "I Like Coffe"
echo json_encode($array);
//>>{"I", "Like", "Coffe"}
?>