PHP JSON decode without foreach - php

how can I get the "avatarmedium" from i json without a foreach Loop?
Thats my current Code:
$json = '{
"response": {
"players": [
{
"steamid": "76561198019025166",
"avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d3/d3ca76e0f1e8d071e826bc3baeebd0e7215e50e2_medium.jpg"
}
]
}
}';
$result = json_decode ($json);
echo $json->response[0]->players->profileurl;
Thank you
with best regards

<?php
$json = '{
"response": {
"players": [
{
"steamid": "76561198019025166",
"avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d3/d3ca76e0f1e8d071e826bc3baeebd0e7215e50e2_medium.jpg"
}
]
} }';
$result = json_decode ($json);
echo $result->response->players[0]->avatarmedium;

Related

Insert new response in PHP

I have two json's Response
{
"response": "00",
"data": {
"Tanggal": "20191118",
"Jam": "144632",
"DestinationName": "NANDA FERNANDO",
"message": "Inquiry success"
}
}
Second one response is
{
"balance" : "1000000"
}
I want to merge them and have a json like
{
"response": "00",
"data": {
"Tanggal": "20191118",
"Jam": "144632",
"DestinationName": "NANDA FERNANDO",
"message": "Inquiry success",
"Balance" : "1000000"
}
}
Code
$ini = json_encode(array_merge(json_decode($response, true),$b));
the response like this
{
"response": "00",
"data": {
"Tanggal": "20191118",
"Jam": "145541",
"DestinationAcc": "7001520304",
"DestinationName": "NANDA FERNANDO",
"MsgKey": "88dc9a20",
"message": "Inquiry success"
},
"balance": "1000000"
}
Maybe you can get an idea from this.
var json1 = '{
"response": "00",
"data": {
"Tanggal": "20191118",
"Jam": "144632",
"DestinationName": "NANDA FERNANDO",
"message": "Inquiry success"
}
}';
var json2 = '{
"balance" : "1000000"
}';
for (var key in json2) {
json1[key] = json2[key]
}
var response = JSON.stringify(json1);
alert(response);
You need to use json_decode(),array_merge() and then json_encode():
<?php
$json = '{
"response": "00",
"data": {
"Tanggal": "20191118",
"Jam": "144632",
"DestinationName": "NANDA FERNANDO",
"message": "Inquiry success"
}
}';
$json2 = '{
"balance" : "1000000"
}';
$arr1 = json_decode($json, true);
$arr2 = json_decode($json2, true);
$arr1['data'] = array_merge($arr1['data'], $arr2);
$data = json_encode($arr1);
echo $data;
Output: https://3v4l.org/bBiis
I see what you are trying to achieve is already achieved , but you want to show it pretty view ( like you snippet ) right ?
you can use JSON_PRETTY_PRINT for that.
This can be simplified more, but for your better understanding,
here it is.
$json = '{
"response": "00",
"data": {
"Tanggal": "20191118",
"Jam": "144632",
"DestinationName": "NANDA FERNANDO",
"message": "Inquiry success"
}
}';
$json2 = '{
"balance" : "1000000"
}';
$arr1 = json_decode($json, true);
$arr2 = json_decode($json2, true);
$arr = array_merge($arr1, $arr2);
$json_string = json_encode($arr, JSON_PRETTY_PRINT);
echo $json_string;
However JSON isn't supposed to contain HTML line breaks, whereas newline characters are valid in JSON. If you want to display JSON on a web page then do a string replacement on the newline characters yourself or else put the JSON in a < pre > element. See json.org for the syntax reference.

PHP Json Parsing 6

How do I pull the 76561198216468627 from
{ "response": { "steamid": "76561198216468627", "success": 1 } }
I've tried this but it pulls 1 instead of the id
foreach ($parsed_json->{'reponse'} as $item) {
$steamid = $item;
}
You need to decode the JSON string into either a PHP object or array. Here's how to do it each way:
ARRAY METHOD
$jsonString = '{ "response": { "steamid": "76561198216468627", "success": 1 } }';
$json = json_decode($jsonString, true);
foreach($json as $item)
{
$steamid = $item['steamid'];
}
OBJECT METHOD
$jsonString = '{ "response": { "steamid": "76561198216468627", "success": 1 } }';
$json = json_decode($jsonString);
foreach($json as $item)
{
$steamid = $item->steamid;
}
Try this:
$json = '{
"response": [{ "steamid": "76561198216468627", "success": 1 }]
}';
$decode = json_decode($json);
echo $decode->response[0]->steamid;
Then you can get 76561198216468627

How to access array inside document using PHP [duplicate]

This question already has answers here:
Get data from JSON file with PHP [duplicate]
(3 answers)
Closed 6 years ago.
I want print specific vars from array inside a document.
JSON structure:
{
"return": {
"string": "2222",
"contacts": [
{
"contact": {
"id": "09890423890"
}
},
{
"contact": {
"id": "2423444"
}
},
{
"contact": {
"id": "24242423"
}
},
etc
]
}
}
I am trying to do this in PHP (and already decoded that json). How can I access and print all ids using foreach or for?
I can not understand how I can manage "return" scope.
You can decode the json contents and use it like an array. This should work:
$json = json_decode($string, true);
$contacts = $json['return']['contacts'];
foreach($contacts as $contact){
echo $contact['contact']['id'];
}
Json decode to array:
$json = json_decode($strjson, true); // decode the JSON into an associative array
and
echo "<pre>";
print_r($json);
And foreach:
foreach ($json as $key => $value) {
echo $key . " ". $value. "<br>";
}
Working example:
$json = '{
"return": {
"string": "2222",
"contacts": [
{
"contact": {
"id": "09890423890"
}
},
{
"contact": {
"id": "2423444"
}
},
{
"contact": {
"id": "24242423"
}
}
]
}
}';
$json = json_decode($json, true);
print_r($json);
foreach ($json['return']['contacts'] as $val) {
echo $val['contact']['id']."<br>";
}
Try this code:
$json ='
{
"return": {
"string": "2222",
"contacts": [
{
"contact": {
"id": "09890423890"
}
},
{
"contact": {
"id": "2423444"
}
},
{
"contact": {
"id": "24242423"
}
},
etc
]
}
}
';
$array = json_decode($json,TRUE);
foreach($array['return']['contacts'] as $value ){
echo $value['id'];
}
Use file_get_contents function if you want to pull data from file.

Php Json Get inside array

This is My json How can get "download" string
{
"total": 1,
"items": [
{
"id": "1186e1472bc1c",
"title": "title,
"desc": "",
"download": "www.google.com",
}
]
}
This is My code:
$response = file_get_contents($nagu);
$obj = json_decode($response, true);
$download = $obj ['items'];
echo $download;
I want output "www.google.com".
$download = $obj ['items'][0]['download'];
echo $download;

Extracting information from an array in php

Can any one tell me how to extract the 'honda' value out of the array below using php?
{
"version": "1.0",
"encoding": "UTF-8",
"entry": {
"name": "bob",
"car": {
"model": "honda"
}
}
}
This looks like a json encoded object. What you could do is:
$info = json_decode($data, true); //where $data has your stuff from the question
$carModel = $obj['entry']['car']['model'];
If you have all that in a variable called "obj", then
$obj = '{ "version": "1.0", "encoding": "UTF-8", "entry": { "name": "bob", "car": { "model": "honda" } } }';
$arr = json_decode($obj, true);
echo $arr['entry']['car']['model'];
should be 'honda'
EDITED: Per Omar below, you do need the true as the second param in json_decode. He should get selected as the correct answer.
Use json_decode as: http://php.net/manual/fr/function.json-decode.php
<?php
$json = '{"version": "1.0","encoding": "UTF-8","entry": {"name": "bob","car": {"model": "honda"} } }';
$tab = json_decode($json, true);
$honda = $tab['entry']['car']['model'];
var_dump($honda);
// or with object:
$obj = json_decode($json);
$honda = $obj->entry->car->model;
var_dump($honda);

Categories