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);
Related
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;
This question already has answers here:
SimpleXML Reading node with a hyphenated name
(2 answers)
Closed 4 years ago.
I have an XML with product. I have a problem with read values witch has a "-" in name because this method dosent work:
$productHurtoID = $product->kod-kreskowy->__toString();
Below You have $product variable:
SimpleXMLElement object {
nazwa => SimpleXMLElement object
kod-katalogowy => (string) K0428
kod-kreskowy => (string) 0027084373370
}
Thanks for help. Kind regards
You could access your dynamically created properties via
$productHurtoID = $product->{'kod-kreskowy'}->__toString();
A different approach would be to pre-process your XML input and replace hyphens with camelCase. But depending on your use-case, this might not be possible.
Regards
You can use following code for getting values with - (hyphen) in key.
$productHurtoID = $product->nazwa->{'kod-kreskowy'}->__toString();
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;
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
This question already has answers here:
How do I access this object property with an illegal name?
(2 answers)
Closed 9 years ago.
For first time I found this problem, see I've a array of objetcs and I access them as follow:
$arrayOfObjects[$pos]->value;
But my problem is that one of the array values have a "-" meaning the value is e-mail so when I do this:
$arrayOfObjects[$pos]->e-mail;
I get an error, how do I handle this? The var can't be changed!!
If you absolutely have to have the hyphens, you can access it like:
$arrayOfObjects[$pos]->{'e-mail'};
You cannot access a variable with a dash in it in that fashion.
Try this:
$arrayOfObjects[$pos]['e-mail'];