Retrieving json encrypted data [duplicate] - php

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 11 months ago.
i got this json encoded data being sent to me.
can u tell me how to get each of the individual elements ?
Something like:
$ticket
$customer
$user
{"ticket":{"id":"10909446","number":"152"},"customer":{"id":"3909381","fname":"","lname":"","email":"me#site.com","emails":["me#site.com"]},"user":{"fname":"Test","lname":"Me","id":17396,"role":"admin"}}
this is a basic view on how my code runs.
$ret = array('html' => '');
$data = json_decode($data , true);
$ret['html'] = '<ul><li>'.$data->ticket->number.'</li></ul>';
echo json_encode($ret);
exit;
it only prints the circle from the li tags.

To clarify #Cthulhu's answer :
$test = '{"ticket":{"id":"10909446","number":"152"},"customer":{"id":"3909381","fname":"","lname":"","email":"me#site.com","emails":["me#site.com"]},"user":{"fname":"Test","lname":"Me","id":17396,"role":"admin"}}';
$data = json_decode($test);
echo $data->ticket->id;
outputs
10909446
json_decode make the JSON into a stdClass object, and then you can access the values as that.
$data = json_decode($test);
$ret = array();
$ret['html']='<ul><li>'.$data->ticket->number.'</li></ul>';
return
json_encode($ret);
will return
{"html":"<ul><li>152<\/li><\/ul>"}

json_decode is the answer for you.

Related

How to get valu of object in php? [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
i have a problem when i try to get the object value there's nothing in it. but when i just echo the object , there's no null i dont know how to get that value
if(isset($_POST['submit'])) {
$secret = "6Le3nvgUAAAAAMU0DRNLtvtaIq3h6X9ybEnO_txv";
$captcha = $_POST['g-recaptcha-response'];
$url = 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$captcha;
$response = file_get_contents($url);
echo $response;
}
i got response
but when i try to $response-> success . it return null ..
The response you 've got is written in JavaScript Object Notation (JSON). For this purpose PHP got it 's own JSON decode and encode functions.
$decodedResponse = json_decode($response);
var_dump($decodedResponse->success);

how to get element from json_encode array response - Codeigniter [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I have a response from my code which looks like this
Controller
$results = $response
echo $result->item;
exit;
Response
{"item":"Samsung A","location":"Hamburg Avenue 5920"}
I want to get only item from the response..How do i achieve this ? The response is json encoded
You need to convert json to object, and after that you can get the element:
$respObject = json_decode($response);
echo $respObject->item;
exit;
json_decode function will helps.
$json_data = '{"item":"Samsung A","location":"Hamburg Avenue 5920"}';
$result = json_decode($json_data);
echo $result->item;
Use json_decode the result will converted into array
$results = json_decode($response);
print_r( $results);
exit;

how to get value from array [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
how to get value from array in php from api
here is my code
$data = callBitAPI('GET','https://min-api.cryptocompare.com/data/pricemultifull?fsyms='.strtoupper($bc->slug).'&tsyms=USD');
if(!empty($data)){
$data = json_decode($data,true);
// echo '<pre>';
// print_r($data);
$data = getRelevantCryptoArray($data,$bc->slug);
}
and here is my result
Please see Attachment
http://prntscr.com/k7hawi
Um... simply
$price = $data['RAW']['BTC']['USD']['PRICE'];
print_r($price);
unless I'm missing something?
dd($data['RAW']['BTC']['USD']['PRICE']) and see the result

How can I access the name value in nested JSON using PHP? [duplicate]

This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
How can I access an array/object?
(6 answers)
Closed 6 years ago.
JSON:
{"location":{"name":"Tirana","region":"Tirane","country":"Albania","lat":41.33,"lon":19.82,"tz_id":"Europe/Tirane","localtime_epoch":1484543668,"localtime":"2017-01-16 5:14"},"current":{"last_updated_epoch":1484543668,"last_updated":"2017-01-16 05:14","temp_c":4.0,"temp_f":39.2,"is_day":0,"condition":{"text":"Overcast","icon":"//cdn.apixu.com/weather/64x64/night/122.png","code":1009},"wind_mph":6.9,"wind_kph":11.2,"wind_degree":150,"wind_dir":"SSE","pressure_mb":1009.0,"pressure_in":30.3,"precip_mm":0.0,"precip_in":0.0,"humidity":60,"cloud":0,"feelslike_c":1.2,"feelslike_f":34.2}}
PHP:
$response = file_get_contents('http://api.apixu.com/v1/current.json?key=a54959ce2b294134bda34330171601&q=Paris');
var_dump(json_decode($response));
echo $response->location[0]->name;
API Call: http://api.apixu.com/v1/current.json?key=a54959ce2b294134bda34330171601&q=Tirana
Try like this.You get the contents in json format.Use json_decode() with second parameter true to convert into array.
<?php
$json = '{"location":{"name":"Tirana","region":"Tirane","country":"Albania","lat":41.33,"lon":19.82,"tz_id":"Europe/Tirane","localtime_epoch":1484543668,"localtime":"2017-01-16 5:14"},"current":{"last_updated_epoch":1484543668,"last_updated":"2017-01-16 05:14","temp_c":4.0,"temp_f":39.2,"is_day":0,"condition":{"text":"Overcast","icon":"//cdn.apixu.com/weather/64x64/night/122.png","code":1009},"wind_mph":6.9,"wind_kph":11.2,"wind_degree":150,"wind_dir":"SSE","pressure_mb":1009.0,"pressure_in":30.3,"precip_mm":0.0,"precip_in":0.0,"humidity":60,"cloud":0,"feelslike_c":1.2,"feelslike_f":34.2}}
';
$array = json_decode($json,true);
//print_r($array);
$location = $array['location'];
echo $location['name'];
?>
use json_decode to parse the json string to array, then access it with the index.
the demo
$response = file_get_contents('http://api.apixu.com/v1/current.json?key=a54959ce2b294134bda34330171601&q=Paris');
$array = json_decode($response, true);
echo $array['location']['name'];

Json php decoding issue [duplicate]

This question already has answers here:
Read Json/Array string in Php
(2 answers)
Closed 8 years ago.
In fact Im having some troubles with php script that i have made recently. The problem is that I can't get data from a json file damo.json using php. This is the code of the json file:
{ "checkouts":[
{
"billing_address":{
"country":"Italy",
"first_name":"christian"
}
},
{
"billing_address":{
"country":"Italy",
"first_name":"christian"
}
}
]
}
i want to get the first_name record. Is it possible using php ?
this is the php code:
<?php
$data = file_get_contents('demo.json');
$obj = json_decode($data);
foreach ($obj->billing_address as $result)
{
echo $result->name;
}
?>
After you fix your syntax as noted above load the file and use json_decode with first parameter the json and second parameter true for associative array.
$data = file_get_contents( "demo.json" );
$data = json_decode($data, true);
var_dump($data);//you have assoc array

Categories