Read Json with php, some troubles [duplicate] - php

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 tried, checked many previous topics, and i cant find the way.
Parsing JSON with PHP
There is the answer, but im blind; actually, im a medimu PHP programmer, but im new with Json.
Here is my Json:
{"result":"true","disponible":{"aaa":"0.00001362","bbb":"0.000392","ccc":"0.00788523","ddd":"0.00004443","eee":"0.0001755","fff":"0.1755","ggg":"797.64618376"}}
My code:
$balances = json_encode(get_balances(),true);
print_r($balances);
The screen show my Json, so everything is ok here. Now, i want take the bolded values from the json and assign it to PHP variables.
$variable1 = $balances["disponible"]["bbb"];
$variable2 = $balances["disponible"]["ggg"];
echo "Valor 1: ".$variable1 ."<br>";
echo "Valor 2: ";$variable2 ;
But it dont work. I tried with many combinantions and nothing.
What im doing wrong?
Thanks a lot in advance. Im blocked with this.

Replace: $balances = json_encode(get_balances(),true); with: $balances = json_decode(get_balances(),true); if you are trying to get associative array.

Related

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)

is there a way to reestructure file_get_contents output? 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.
Im triyng to take some information from a json file,
the code in that page is like this
{"asignaciones":[{"fecha":"LUNES
29/07","horaEntrada":"18:30","horaSalida":"22:30","tienda":" BK Villa
Urquiza"}],"fechaConsulta":"29/07/2019 17:27","legajo":"28907"}
i want to take "29/7", "18:30", "22:30".
For now, i can print all the code in my page, but i want to take only those numbers, there is a way with:file_get_contents?
i'm trying to learn a little more php sorry if this question is oviously simple.
my code now:
$content = file_get_contents('http://proveedores.alsea.com.ar:48080/asignaciones-server/mobile/main/asignaciones/legajos/28907');
echo $content
?>
It looks like the content is a json... Start from this:
$content = file_get_contents('http://proveedores.alsea.com.ar:48080/asignaciones-server/mobile/main/asignaciones/legajos/28907');
$content = json_decode($content);
print_r($content);

Get variable with PHP from JSON [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I have been scratching my head with this one and wonder if anybody would be kind enough to give me a pointer. I am extracting some data as a variable from JSON to PHP, and I can do this no problem when there are nested nodes - IF the node is a text but not if the node is a number. I am using json_decode
THIS IS NOT THE SAME AS How do I extract data from JSON with PHP?
This is ok
$get_temp = $jsonobj->main->temp;
This is not working
$get_weather = $jsonobj->main->0->weather;
So my question is how do I target the node when it is a number?
Thanks
Probabily you have an array into main node.. so you can get its value with an index like this:
$get_weather = $jsonobj->main[0]->weather;
Where 0 is the index that you want to get
$get_weather = $jsonobj->main[$x]->weather;
$x would be the index
This should work:
$get_weather = $jsonobj->main[0]->weather;

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;

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