This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I want to echo a certain part of json in php. I don't know if the json data is valid or invalid.
I have tried the below code:
$get = get_content('domain');
$json = file_get_contents($get);
$decode = json_decode($json);
$final = $decode->count;
echo $final;
The json data which I get from an URL is:
{"15":{"xy":{"cost":6,"count":235}}}
I expect to see only the 235 part. This data keeps on changing. Sometimes the number 6 in the cost can vary as well.
EDIT: Fixed some code.
EDIT1: Actually the data was already a string so just used echo substr($get, 30, 3); to achieve the results.
Try this, it should work
echo $decode[15]['xy']['count'];
Related
This question already has answers here:
How to display json values that are in 2nd level of a multi-dim array?
(3 answers)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
Need help. I am having trouble solving this json thingy. here's what is on my database
{"img":["images/logo2.png","images/logo.png"]}
I need to decode this images and show it like this
images/logo2.png
images/logo.png
A quick google search will give you this link: json_decode.
$someJson = '{"img":["images/logo2.png","images/logo.png"]}';
$encodeJson = json_decode($someJson, true);
echo $encodeJson['img'][0]; //images/logo2.png
echo $encodeJson['img'][1]; //images/logo.png
Hint: Having a look at the PHP-Manual is not prohibited. ;)
Maybe select your entry from database and put in a variable called $dataBaseEntry then copy paste this code?
$decoded = json_decode($dataBaseEntry, true); //true for associated array
$logos = $decoded['img'];
print_r($logos)
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I want to apply the regex to extract only the values. I am not getting the perfect one any help
[{"name":"basket ball"},{"name":"foot ball"},{"name":"sports"}]
There's absolutely no need for a regex here. Use json_decode():
$string = '[{"name":"basket ball"},{"name":"foot ball"},{"name":"sports"}]';
$data = json_decode($string, true);
now you have a normal php array $data to get your wanted data from.
like
echo $data[0]['name']; // basket ball
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 a JSON output that looks like this:
{"data":[{"QID":"Q1234","MgrQID":"5678","NTID":"blah"}]}
I am trying to access just the data within the square brackets, inside the data array?
I have tried $jsonVar['data'] and $jsonVar->data and neither of them are working.
Is there a way I can just get access to [{"QID":"Q1234","MgrQID":"5678","NTID":"blah"}]?
Try this example:
<?php
$data = '{"data":[{"QID":"Q1234","MgrQID":"5678","NTID":"blah"}]}';
$test = json_decode($data, true);
echo json_encode($test['data']);
?>
Output:
[{"QID":"Q1234","MgrQID":"5678","NTID":"blah"}]
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
json decode in php
I want to get my id in php, actually without that php sdk files.
So i am using this.
$a = file_get_contents('https://graph.facebook.com/100004304380055...');
echo $a['id'];
The contents of the response are
{
"id": "100004304380055",
"name": "Ayush Mishra"
}
But it does not returns my id 100004304380055. I thing $a is not an array. I want to know how to make $a an array and then get my id by $a['id']. Please provide the code. Any help will be appreciated.
The endpoint is returning a JSON string; which is a format used to convey object information as a string. Convert the string back to an object, and grab the id property:
$data = json_decode($response);
echo $data->id;
Where $response is the result of your call to file_get_contents().
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Extract JSONP Resultset in PHP
I get the response in the following format. Im facing trouble on how to get inside "Plugin" variable and access other variables inside it.I used json_decode(), but i cant access the variables.
Plugin
(
{
"plugin_a":"abc",
"plugin_b":"abc",
"plugin_c":"abc"
}
)
I tried
$a = json_decode($json,true);
echo $a['plugin_a'];
I dont get any output.
echo var_dump($json); gives me
string 'Plugin({
"plugin_a":"abc",
"plugin_b":"abc",
"plugin_ce":"abc" })'
try substr();
http://sandbox.phpcode.eu/g/40c20.php
<?php
$json = substr('Plugin
(
{
"plugin_a":"abc",
"plugin_b":"abc",
"plugin_c":"abc"
}
)', 9, -1);
print_r(json_decode($json));
Perhaps this will work for you:
$data=array('plugin_a'=>'abc','plugin_b'=>'bcd','plugin_c'=>'cde');
$json='{"Plugin":'.json_encode($data).'}';
$a=json_decode($json,true);
echo $a['Plugin']['plugin_a'];
It appears as though the actual json array may not have integrity. If this solution doesn't fit, can you post the code that actually builds the json array?