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'];
Related
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);
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:
What is the syntax for accessing PHP object properties? [closed]
(3 answers)
Closed 3 years ago.
I'm lost by retrieving my PHP var. I've tried multiple things without success.
My var is called $answer and here is his var_dump :
object(stdClass)#3631 (1) { ["token"]=> string(159) "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOjEsImlhdCI6MTQ2MTMyMDQzNCwic3ViIjoiQ2hhdEF1dGhlbnRpZmljYXRpb24ifQ.18jpKLj_6Banyncyq6bz9jIFSB3IRDpBCvSgpIGJPrs" }
The most logic is to access by $answer["token"] But it's not working.
How can i get my data ?
This doesn't look like an array, but an object of stdClass. Use the Object Operator to access it:
$answer->token;
You can access it like this: $answer->token
You can access by $answer->token; instead of $answer["token"], as it is an object - not an array
This question already has answers here:
Mixing a PHP variable with a string literal
(5 answers)
Closed 9 years ago.
I have a variable and I want to create a variable with that. I get the variable from database and put it together with some text and then I want another variable.
For exampel
$a = $ . "txt" . $d;
Try with this. It will create a variable from another one.
$a = ${'txt'.$d}
P.s. This is a question asked a couple of times. You might have found the answer simply by searching the issue on google.
This question already has answers here:
PHP Object Property has brackets in it
(2 answers)
Closed 9 years ago.
I'm doing a print_r on an object that is returned after running a query. Here is my print_r log.
stdClass Object
(
[MAX(sort_order)] => 3
)
I want to get the value inside [MAX(sort_order)] but I cant figure out how to target it from within php.
Like $sort_order = $object->[MAX(sort_order)]; (I know that won't work)
Does anyone know how I can do this?
Try add this in your query MAX(sort_order) AS max_sort_order in your query