Display json array [duplicate] - php

This question already has answers here:
How do I access this object property with an illegal name?
(2 answers)
Closed 10 months ago.
I am trying to read an api I have implemented with Wordpress using WP-API V2, I have used several plugins to return the information i need.
The source json can be found here.
I need to return pure_taxonomies->property-status->name.
I have tried the following but i just get a blank page:
foreach($select_api as $p)
{
echo '
Status:'.$p->pure_taxonomies->property-status->name.'
';
}
Any help would be great!

First of all, if you have json_encoded string, you should decode it with
json_decode()
I decoded your json and get array with two elements.
Your error is probably because of hyphen property-status in name of property.
You should use curly braces:
Status:'.$p->pure_taxonomies->{"property-status"}[0]->name.'
IMPORTANT.
Use curly braces for property names with hyphen
Don't forget that in your structure property-status is array. That's why I used index 0 to get first element

Related

Is there a way to escape the "." character in php? [duplicate]

This question already has answers here:
Dealing with special characters in object property names
(1 answer)
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 1 year ago.
I have an stdClass object that I have dynamically created from a JSON using json_decode(). I am trying to access a field by calling $value->V.X->processedField
but this field has a period. This is giving me a syntax error. Is there a way to somehow escape the period in my code, or am I going to have to rename the field in my JSON?
<?php
// example code
$value = json_decode('{"V.X": {"processedField":"the value"}}');
print_r($value->{"V.X"}->processedField);
// or
$var = "V.X";
print_r($value->$var->processedField);

Access to object property 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'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;

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;

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;

Categories