How to create an array from simple string? [duplicate] - php

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().

Related

How do I echo the json data in php? [duplicate]

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'];

json_decode for PHP joomla [duplicate]

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)

can't access object in array PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
Have tried every variation I can think of to access an object property in an array. I'm getting some data back form an API which i'm storing in a variable called $userTokenValid:
$userTokenValid = [{"authTokenValid":1}];
i'm then trying to access the authTokenValid property like so:
echo json_decode($userTokenValid[0]->authTokenValid);
I appreciate this might be quite basic but can't spot where I have gone wrong.
$userTokenValid isn't valid php. However [{"authTokenValid":1}] is a valid json string.
$userTokenValid = '[{"authTokenValid":1}]';
you can decode it with
$json = json_decode($userTokenValid);
finally
echo $json[0]->authTokenValid;

parse multilevel json in php [duplicate]

This question already has answers here:
Trying to parse JSON with PHP
(2 answers)
Accessing JSON array after json_decode/multidimensional array
(3 answers)
Closed 8 years ago.
I am receiving a multilevel json like this:
{"id":"3",
"field2":"ssss",
"field3":[
{"field31":"wwwww",
"field32":"qqqq"},
{"field31":"wwwww",
"field32":"qqqq"},
{"field31":"wwwww",
"field32":"qqqq"},
]}
I am reading json values (first level) like this in php:
$json = file_get_contents('php://input');
$json_post = json_decode(utf8_decode($json), true);
$id = $json_post['id'];
But I don't know how to get the json in the second level. What is the simplest way? I just need to get the json in other variable like $json_post, I will read it in a for bucle to get each row of the json file
It's going to give you a PHP object if you run json_decode(utf8_decode($json));
echo $json_post->field3->field31;
EDIT: I missed that you were using the conversion to array. This is what the array would look like
echo $json_post['field3']['field31'];

How to parse JSONP with PHP [duplicate]

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?

Categories