DekGenius.com
PHP
php convert array to json object
$myArr = array("apple", "banana", "mango", "jackfruit");
$toJSON = json_encode($myArr);
echo $toJSON;
json to array php
//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);
How to JSON encode a PHP array
<?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);
?>
how to receive json data in php
$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
echo $data;
json to php array
<?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>';
?>
php json data to array
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.
json php
<?php
header("Content-Type: application/json; charset=utf-8");
if (!empty($_REQUEST['q'])){
$q = $_REQUEST['q'];
require_once('api-key.php');
$apiUrl = "http://api.openweathermap.org/data/2.5/weather?q=" . $q . "&lang=fr&units=metric&APPID=" . API_KEY;
$response = file_get_contents($apiUrl, False);
$data = json_decode($response, true); // $data = TABLEAU PHP
setLocale(LC_TIME,"fr_FR.UTF-8");
date_default_timezone_set("Europe/Paris");
$today = strftime('%A %d %B %y',time());
$hour = date('H:i:s');
// on prépare un tableau $json pour la réponse
$json = array("lieu" => $q,
"jour" => $today,
"heure"=> $hour,
"meteo"=> array());
$json['meteo']['main'] = $data['main'];
$json['meteo']['description'] = $data['weather'][0]['description'];
$json['meteo']['id'] = $data['weather'][0]['id'];
echo json_encode($json,JSON_PRETTY_PRINT);
}
php convert array to json
return json in php
//code igniter
$query="qry";
$query = $this->db->query($query);
$res=$query->result();
return json_encode($res);
php object to json
function getJsonData(){
$var = get_object_vars($this);
foreach ($var as &$value) {
if (is_object($value) && method_exists($value,'getJsonData')) {
$value = $value->getJsonData();
}
}
return $var;
}
json en php
{"data":[
{ "sub_data1":"value1", "sub_data2":"value2","sub_data_n":"value n" },
{ "sub_data2":"value2","sub_data2":"value2", "sub_data_n":"value n" },
{ "sub_data n":"value n ", "sub_data2":"value2","sub_data_n":"value n" }
]}
© 2022 Copyright:
DekGenius.com