This question already has answers here:
PHP foreach change original array values [duplicate]
(5 answers)
How to update/edit a JSON file using PHP [closed]
(1 answer)
Closed 2 years ago.
Please help me!
JSON:
{
"1":{
"TchID":"G303992",
"TchData":{
"TchID":"G303992",
"TchNama":"G303992",
"TchPassword":43511824
}
},
"2":{
"TchID":"G141843",
"TchData":{
"TchID":"G141843",
"TchNama":"G141843",
"TchPassword":22932450
}
}
}
How to update value in nested object from
"2":{
"TchID":"G141843",
"TchData":{
"TchID":"G141843",
"TchNama":"G141843",
"TchPassword":22932450
}
}
to
"2":{
"TchID":"G141843",
"TchData":{
"TchID":"G141843",
"TchNama":"Alex J",
"TchPassword":22932450
}
}
in php script?????
You see that i want to change "TchNama":"G141843" to "TchNama":"Alex J"
Here my code
<?php
$data = '{"1":{"TchID":"G303992","TchData":{"TchID":"G303992","TchNama":"G303992","TchPassword":43511824}},"2":{"TchID":"G141843","TchData":{"TchID":"G141843","TchNama":"G141843","TchPassword":22932450}}}';
$guru = json_decode($data);
foreach ($guru as $items){
if($items['TchID'] == 'G303992'){
// i dont know how to change $items['TchData']['TchNama'] = 'G141843' to 'Alex J'
$viewchange = json_encode($guru);
echo $viewchange;
}
}
?>
You can covert the json string to an array-object using json_encode($,TRUE).
Then you will be able to loop through the keys of the object if you obtain the keys by array_keys().
And then you can either use a separate variable to iterate through the main object properties and change that object, or directly access the main object and change it at the source, which is what I did below:
$data = '{"1":{"TchID":"G303992","TchData":{"TchID":"G303992","TchNama":"G303992","TchPassword":43511824}},
"2":{"TchID":"G141843","TchData":{"TchID":"G141843","TchNama":"G141843","TchPassword":22932450}}}';
$guru = json_decode($data, true);
$keys = array_keys($guru);
foreach ($keys as $key) {
if($guru[$key]['TchID'] == 'G303992'){
$guru[$key]["TchNama"] = "Alex J";
}
}
$viewchange = json_encode($guru);
echo $viewchange;
Please try this.
$data = '{"1":{"TchID":"G303992","TchData":{"TchID":"G303992","TchNama":"G303992","TchPassword":43511824}},"2":{"TchID":"G141843","TchData":{"TchID":"G141843","TchNama":"G141843","TchPassword":22932450}}}';
$guru = json_decode($data);
var_dump($guru);
foreach ($guru as $items) {
if ($items->TchID == 'G141843') {
$items->TchData->TchNama = "Alex J";
}
}
var_dump($guru);
$viewchange = json_encode($guru);
echo $viewchange;
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 1 year ago.
I have a url that returns a json. I want to loop through to get each of the values in the json.
This is what my json looks like.
{"error":false,
"message":"Request orders successfully completed",
"orders":[{
"oid":505,
"uid":234,
"total_amount":"143.99000"
},
{
"oid":506,
"uid":234,
"total_amount":"1.19000"
}]
}
My PHP code
$getOrdersUrl = file_get_contents("https://apiurl?last_id=504");
$json = json_decode($getOrdersUrl, true);
if(!empty($json)){
foreach($json as $val)
{
$oid = $val['orders'][0]['oid'];
$uid = $val['orders'][0]['uid'];
$total_amount = $val['total_amount'];
echo $oid;
}
}else{
echo "No Data to fetched to fetch for <br>";
}
<?php
$json = file_get_contents('https://api.jsonbin.io/b/6172d48d9548541c29c6ff05');
$data = json_decode($json, true);
foreach ($data['orders'] as $key => $value) {
echo "oid = ".$value['oid']."<br>";
echo "uid = ".$value['uid']."<br>";
echo "total_amount = ".$value['total_amount']."<br>";
}
?>
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I am new to php. I trying to get value from json object.
I tried many time but i failed to get value from json. I need only "txnToken" from json array.
My code is
$json = json_decode($data, true);
foreach ($json as $key => $value) {
foreach ($value as $key1 => $value1) {
print_r($key1);
}
}
And JSON Response is:
{
"head": {
"responseTimestamp":"1596640639585",
"version":"v1",
"clientId":"WEB",
"signature":"xxxxxxxxxxxxxxxx"
},
"body":{
"resultInfo":{
"resultStatus":"S",
"resultCode":"0000",
"resultMsg":"Success"
},
"txnToken":"xxxxxxxxxxxxx",
"isPromoCodeValid":false,
"authenticated":false
}
}
Thanks in advance.
I guess it should be as easy as this:
$json = json_decode($data, true);
print_r($json['body']['txnToken']);
Does it work if you do
$json['body']['txnToken']
This question already has answers here:
How to convert a string to JSON object in PHP
(6 answers)
Closed 4 years ago.
I have created a Soap client to request data from web services and the returned data is displayed like this with my CodeIgniter function"
{"GetCodeResult":
{
"Selling":"1000.67114",
"Buying":"9000.65789"
}
}
but I want to format them like this by removing the getCodeResult
{"selling":"1000.67114","Buying":"9000.65789"}
I hope this may help you.
<?php
$jsonData = '{"GetCodeResult":
{
"Selling":"1000.67114",
"Buying":"9000.65789"
}
}';
$data = json_decode($jsonData, true);
$arr_index = array();
foreach ($data as $key => $value) {
$arr_index = $value;
}
echo json_encode($arr_index, true);
?>
Thanks.
If you want to get only the values of selling and buying means, try like below.
<?php
$jsonData = '{"GetCodeResult":
{
"Selling":"1000.67114",
"Buying":"9000.65789"
}
}';
$data = json_decode($jsonData, true);
$arr_index = array();
foreach ($data as $key => $value) {
if($key == 'GetCodeResult'){
$arr_index = $value;
}
}
foreach($arr_index as $values){
$finalVal[] = $values;
}
echo json_encode($finalVal, true);
?>
Decode to array and return only the 'GetCodeResult' then json_encode it.
echo json_encode(json_decode($json, true)['GetCodeResult']);
//{"Selling":"1000.67114","Buying":"9000.65789"}
https://3v4l.org/59RSc
To only echo the values you can assign them to variables using list and array_values.
list($selling, $buying) = array_values(json_decode($json, true)['GetCodeResult']);
echo $selling .PHP_EOL;
echo $buying .PHP_EOL;
//1000.67114
//9000.65789
https://3v4l.org/T5GNg
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I have json data like this:
{"result":{"total":19,"hits":[{"keyword":"sidney webb"},{"keyword":"dr webb"}]},"status_msg":"OK","status_code":200,"left_lines":1196851}
I need to extract from json only keywords. I try without success with code:
$json = '{"result":{"total":19,"hits":[{"keyword":"sidney webb"},{"keyword":"dr webb"}]},"status_msg":"OK","status_code":200,"left_lines":1196851}';
foreach ($json['result'] as $keywords)
{
echo $keywords['result']['keyword'];
}
Seems, you forgot to decode the string with json_decode. In addition, your likely would to loop over [result][hits] and not just [result]
$json = '{"result":{"total":19,"hits":[{"keyword":"sidney webb"},{"keyword":"dr webb"}]},"status_msg":"OK","status_code":200,"left_lines":1196851}';
$json = json_decode($json, true);
foreach ($json['result']['hits'] as $hit)
{
echo $hit['keyword'];
}
TRY
$json = '{"result":{"total":19,"hits":[{"keyword":"sidney webb"},{"keyword":"dr webb"}]},"status_msg":"OK","status_code":200,"left_lines":1196851}';
$jsonp =json_decode($json,TRUE);
foreach ($jsonp['result'] as $val)
{
if(is_array($val) ){
foreach ($val as $k)
{
echo $k['keyword'];
}
}
}
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 4 years ago.
I'm trying to get data from the following JSON file using PHP. I specifically want "temperatureMin" and "temperatureMax".
It's probably really simple, but I have no idea how to do this. I'm stuck on what to do after file_get_contents("file.json"). Some help would be greatly appreciated!
{
"daily": {
"summary": "No precipitation for the week; temperatures rising to 6° on Tuesday.",
"icon": "clear-day",
"data": [
{
"time": 1383458400,
"summary": "Mostly cloudy throughout the day.",
"icon": "partly-cloudy-day",
"sunriseTime": 1383491266,
"sunsetTime": 1383523844,
"temperatureMin": -3.46,
"temperatureMinTime": 1383544800,
"temperatureMax": -1.12,
"temperatureMaxTime": 1383458400,
}
]
}
}
Get the content of the JSON file using file_get_contents():
$str = file_get_contents('http://example.com/example.json/');
Now decode the JSON using json_decode():
$json = json_decode($str, true); // decode the JSON into an associative array
You have an associative array containing all the information. To figure out how to access the values you need, you can do the following:
echo '<pre>' . print_r($json, true) . '</pre>';
This will print out the contents of the array in a nice readable format. Note that the second parameter is set to true in order to let print_r() know that the output should be returned (rather than just printed to screen). Then, you access the elements you want, like so:
$temperatureMin = $json['daily']['data'][0]['temperatureMin'];
$temperatureMax = $json['daily']['data'][0]['temperatureMax'];
Or loop through the array however you wish:
foreach ($json['daily']['data'] as $field => $value) {
// Use $field and $value here
}
Demo!
Use json_decode to transform your JSON into a PHP array. Example:
$json = '{"a":"b"}';
$array = json_decode($json, true);
echo $array['a']; // b
Try:
$data = file_get_contents ("file.json");
$json = json_decode($data, true);
foreach ($json as $key => $value) {
if (!is_array($value)) {
echo $key . '=>' . $value . '<br/>';
} else {
foreach ($value as $key => $val) {
echo $key . '=>' . $val . '<br/>';
}
}
}