Main /
Php-json
@
[
{
"Code": "1",
"Name": "June Zupers",
"Sports": "Base Ball"
},
{
"Code": "2",
"Name": "Fred Cortez",
"Sports": "Soccer"
},
{
"Code": "3",
"Name": "Kevin Burks",
"Sports": "Tennis"
}
]
--------read--------------------
<?php
// load file
$data = file_get_contents('results.json');
// decode json to associative array
$json_arr = json_decode($data, true);
foreach ($json_arr as $key => $value) {
echo $json_arr[$key] . " - " . $json_arr[$value] . "<br/>";
}
?>
-------add--------------------
<?php
// read json file
$data = file_get_contents('results.json');
// decode json
$json_arr = json_decode($data, true);
// add data
$json_arr[] = array('Code'=>4, 'Name'=>'Jeff Darwin', 'Sports'=>'Cricket');
// encode json and save to file
file_put_contents('results_new.json', json_encode($json_arr));
?>
-------update--------------------------
<?php
// read file
$data = file_get_contents('results.json');
// decode json to array
$json_arr = json_decode($data, true);
foreach ($json_arr as $key => $value) {
if ($value['Code'] == '2') {
$json_arr[$key]['Sports'] = "Foot Ball";
}
}
// encode array to json and save to file
file_put_contents('results_new.json', json_encode($json_arr));
?>
---------delete--------------------------------
<?php
// read json file
$data = file_get_contents('results.json');
// decode json to associative array
$json_arr = json_decode($data, true);
// get array index to delete
$arr_index = array();
foreach ($json_arr as $key => $value)
{
if ($value['Code'] == "2")
{
$arr_index[] = $key;
}
}
// delete data
foreach ($arr_index as $i)
{
unset($json_arr[$i]);
}
// rebase array
$json_arr = array_values($json_arr);
// encode array to json and save to file
file_put_contents('results_new.json', json_encode($json_arr));
?>
-----------------------------
$body = array(
array(
"id"=> 1,
"city"=> "Sorong",
"positif"=> ang($hh[117]),
"sembuh"=> ang($hh[120]),
"meninggal"=> ang($hh[123]),
),
array(
"id"=> 2,
"city"=> "Kota Sorong",
"positif"=> ang($hh[225]),
"sembuh"=> ang($hh[228]),
"meninggal"=> ang($hh[231]),
),
);
output-output-output-
[{"id":1,"city":"Sorong","positif":"589","sembuh":"529","meninggal":"14"},{"id":2,"city":"Kota Sorong","positif":"3241","sembuh":"3148","meninggal":"63"},{"id":3,"city":"Manokwari","positif":"3528","sembuh":"3446","meninggal":"43"},{"id":4,"city":"Fak-Fak","positif":"108","sembuh":"97","meninggal":"11"},{"id":5,"city":"Sorong Selatan","positif":"185","sembuh":"170","meninggal":"14"},{"id":6,"city":"Kaimana","positif":"112","sembuh":"110","meninggal":"2"},{"id":7,"city":"Teluk Wondama","positif":"311","sembuh":"298","meninggal":"6"},{"id":8,"city":"Teluk Bintuni","positif":"860","sembuh":"842","meninggal":"14"},{"id":9,"city":"Raja Ampat","positif":"343","sembuh":"342","meninggal":"0"},{"id":10,"city":"Maybrat","positif":"12","sembuh":"9","meninggal":"3"},{"id":11,"city":"Pegunungan Arfak","positif":"3","sembuh":"3","meninggal":"0"},{"id":12,"city":"Manokwari Selatan","positif":"91","sembuh":"89","meninggal":"2"},{"id":13,"city":"Tambrauw","positif":"14","sembuh":"13","meninggal":"1"}]
curl-header
function getx(){
$abear = "AAAAAAAAAAAAAAAAAAAxxxxxxxxxxxxxxxxxxxxx";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=5&screen_name=infoBMKG&include_entities=true&trim_user=1%0A',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '.$abear,
),
));
$res = curl_exec($curl);
curl_close($curl);
$js = json_decode($res, true);
$x = 0;
while($x < count($js)) {
$idx = $js[$x]['id_str'];
$tx = $js[$x]['text'];
$tx = explode("Gempa", $tx);
echo count($tx)."<br>";
$x++;
}
}////