Access to object property php [duplicate] - php

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
I'm trying to acces to range but is tnot working
$json = '{"range":[[1,2,20],[3,4.5]]}';
var_dump(json_decode($json->'range'));//doesn't work
var_dump(json_decode($json['range']));//doesn't work
Whay is the way to acces to range?

You need to json_decode() your JSON string first to turn it into an object. After that you can access the object properties with the -> operator.
So the correct way is this
var_dump(json_decode($json)->range));
But it is more readable if you split it into multiple statements:
$decoded = json_decode($json);
var_dump($decoded->range);

you must use this one.
json_decode($json)->range;

Related

How to convert cURL response into array [duplicate]

This question already has answers here:
json_decode to array
(12 answers)
Closed 2 years ago.
Hello I am trying to get the response from the cURL call to stack exchange api and want to convert the json response into a php array, is there a example code to show how to get this converted.
You can use json_decode() on the json object.
I highly recommand to give true in the second arg:
$php_object = json_decode($api_response, true);
True give you an associative array. I think it's useful for your case

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)

Extract the values from the array which is inside the object [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
if i solve this i will have my desired result for any expert this will take few minutes to guess and i am stuck in this from last 2 hours searching and googling but couldn't found the one i want. Okay so here is the thing ...
I am sending data through ajax to my php by doing JSON.stringify and receving that that to my php in the form of this
{"0":["BE","test","test","1993"],"1":["HSSC","test","test","1995"]}
All i want to do is to get the values and insert them to the separate variables.
You need to use json_decode to convert the string to array. You can pass the second parameter to this method to convert this to array instead of object. Then you can retrieve the data like
$string = '{"0":["BE","test","test","1993"],"1":["HSSC","test","test","1995"]}';
$array = json_decode($string, true);
print_r($array[0][0]);
Here's the example of it https://repl.it/HhI8/1

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;

unserialize.com how they do it [duplicate]

This question already has answers here:
What kind of string is this? How do I unserialize this string? [duplicate]
(2 answers)
Closed 6 years ago.
I got the following string:
a:16:{i:0;s:1:"6";i:1;s:2:"12";i:2;s:1:"2";i:3;s:2:"11";i:4;s:1:"7";i:5;s:2:"10";i:6;s:2:"16";i:7;s:1:"5";i:8;s:1:"3";i:9;s:1:"1";i:10;s:2:"14";i:11;s:1:"8";i:12;s:2:"13";i:13;s:1:"9";i:14;s:1:"4";i:15;s:2:"15";}
I can't use
var_dump(json_decode($json))
because it doesn't return anything. I found 2 websites that where a bit usefull:
http://jsonlint.com/ for checking if the object is valid (what it ain't) and http://www.unserialize.com/ wich can 'unserialize' the json string back to an array.
Now I wonder what unserialize does to the json string. So I can use it in my script as well.
Looks like this is in serialized form. You need to use unserialize() function here.
$input = 'a:16:{i:0;s:1:"6";i:1;s:2:"12";i:2;s:1:"2";i:3;s:2:"11";i:4;s:1:"7";i:5;s:2:"10";i:6;s:2:"16";i:7;s:1:"5";i:8;s:1:"3";i:9;s:1:"1";i:10;s:2:"14";i:11;s:1:"8";i:12;s:2:"13";i:13;s:1:"9";i:14;s:1:"4";i:15;s:2:"15";}';
var_dump(unserialize($input));
Use unserialize function in php
<?php
echo '<pre>';
print_r(unserialize('a:16:{i:0;s:1:"6";i:1;s:2:"12";i:2;s:1:"2";i:3;s:2:"11";i:4;s:1:"7";i:5;s:2:"10";i:6;s:2:"16";i:7;s:1:"5";i:8;s:1:"3";i:9;s:1:"1";i:10;s:2:"14";i:11;s:1:"8";i:12;s:2:"13";i:13;s:1:"9";i:14;s:1:"4";i:15;s:2:"15";}'));
?>
https://eval.in/637790

Categories