xxxxxxxxxx
$myArr = array("apple", "banana", "mango", "jackfruit");
$toJSON = json_encode($myArr);
echo $toJSON;
xxxxxxxxxx
//2 ways
//this is for string from $_REQUEST,$_POST to array
$jsonText = $_REQUEST['myJSON'];
$decodedText = html_entity_decode($jsonText);
$myArray = json_decode($decodedText, true);
//this is for json to array
$assosiative_array = json_decode(json_encode($jsonText),true);
xxxxxxxxxx
<?php
// the php array
$array = array();
$array['key1'] = "value1";
$array['key2'] = "value2";
$array['key3'] = "value3";
// encode the php array into JSON format
$json = json_encode($array);
// check out the results
var_dump($json);
?>
xxxxxxxxxx
<?php
$json = '[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}]';
$json_decoded = json_decode($json);
echo '<table>';
foreach($json_decoded as $result){
echo '<tr>';
echo '<td>'.$result->name.'</td>';
echo '<td>'.$result->phone.'</td>';
echo '<td>'.$result->email.'</td>';
echo '</tr>';
}
echo '</table>';
?>
xxxxxxxxxx
json_decode('{foo:"bar"}'); // this fails
json_decode('{"foo":"bar"}', true); // returns array("foo" => "bar")
json_decode('{"foo":"bar"}'); // returns an object, not an array.
xxxxxxxxxx
[
"db" => [
"table" => "report_data",
"field" => "id_report"
],
"fields" => [
[
[
"city" => "name"
],
"id_city",
"city"
],
[
[
"federal_subject_name" => "name"
],
"id_region",
"region"
],
[
[
"area" => "name"
],
"id_area",
"area"
],
[
[
"inn" => "inn",
"owner" => "name"
],
"id_owner",
"owner"
]
]
];