1phpCopy<?php
2 $array = ["Lili", "Rose", "Jasmine", "Daisy"];
3 $JsonObject = json_encode($array);
4 echo "The array is converted to the Json string.";
5 echo "\n";
6 echo"The Json string is $JsonObject";
7?>
8
1Using implode() function in Php
2-----------------------
3Syntax
4implode(separator,array);
5
6Example
7<?php
8//assigning value to the array
9$dummyArr = array("Hello","Greppers,","Ankur","here !");
10
11echo implode(" ",$dummyArr);// Use of implode function
12?>
13
14Output:
15Hello Greppers, Ankur here !
1phpCopy<?php
2 $array = ["Lili", "Rose", "Jasmine", "Daisy"];
3 $JsonObject = serialize($array);
4 echo "The array is converted to the Json string.";
5 echo "\n";
6 echo"The Json string is $JsonObject";
7?>
8
1$person = [
2 'name' => 'Jon',
3 'age' => 26,
4 'status' => null,
5 'friends' => ['Matt', 'Kaci', 'Jess']
6];
7
8echo json_encode($person);
9// {"name":"Jon","age":26,"status":null,"friends":["Matt","Kaci","Jess"]}
10
1function subArraysToString($ar, $sep = ', ') {
2 $str = '';
3 foreach ($ar as $val) {
4 $str .= implode($sep, $val);
5 $str .= $sep; // add separator between sub-arrays
6 }
7 $str = rtrim($str, $sep); // remove last separator
8 return $str;
9}
10
11// $food array from example above
12echo subArraysToString($food);
13// apple, raspberry, pear, banana, peas, carrots, cabbage, wheat, rice, oats
14