reference object with key containing brackets [duplicate] - php

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

Related

Why does an object behave differently after a print_r / var_dump [duplicate]

This question already has answers here:
print_r() adds properties to DateTime objects [duplicate]
(7 answers)
Why can't I access DateTime->date in PHP's DateTime class?
(5 answers)
Closed 5 years ago.
Can anyone explain why these two pieces of code act differently and what I'm doing wrong:
print_r($user['first_signed_up_date']);
echo $user['first_signed_up_date']->milliseconds/1000;
Result:
MongoDB\BSON\UTCDateTime Object
(
[milliseconds] => 1507976375000
)
1507976375
Second code:
// removed print_r / var_dump
echo $user['first_signed_up_date']->milliseconds/1000;
Result:
0
The only difference between first and second code is there's no print_r (var_dump has same result)

How to access to my PHP var [duplicate]

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

Fetching data from array inside array [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 7 years ago.
I've finally figured out how to fetch data from Google Analytics to website, unfortunately output (print_r) is PHP array inside an array:
Array ( [0] => Array ( [0] => 196 ) )
I need to get the number 196 & "save" it as variable in order to use it in calculations or output it, any ideas how to achieve this?
Im looking for overall solution/principle/rule because some data is even array inside array inside array etc.
you need to grab the first array element -position 0 - and from this inner array you pick the first element - also position 0 - which is 196, Check this: PHP Fiddle - hit run to execute
// getting 196
echo $myArr[0][0];
Use this:
$newVariable = $yourArray[0][0];
echo $newVariable; //This will echo the answer.
//This answer is self explanatory.

PHP array value into second array [duplicate]

This question already has answers here:
Forcing a SimpleXML Object to a string, regardless of context
(11 answers)
Closed 9 years ago.
I've been searching around but i'm failing to find an answer to something that seems it should be simple to fix!
I'm reading products from XML and putting their data into an array on a loop, the array is called $res.
Now, i need to put a value from $res into another array for loading to the DB (magento SOAP API). But when i do this i do not get a string value i wxpect, instead i get an the first array inside the second.
Here is the problem line:
$fieldDateData = array('rts_date'=>$res[0]->BackInStockDate1);
I've tried a few different things, none have worked. I thought it would be simply enough to do this:
$data = $res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);
But sadly not, i'm unsure as to why?
Thanks,
EDIT:
This is an example of the output
Array
(
[rts_date] => SimpleXMLElement Object
(
[0] => 28/06/13
)
)
Try
$data = (string)$res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);
You need to cast the value you are setting as a string:
$data = (string) $res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);

PHP get index of object [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get Instance ID of an Object in PHP
I'm new to OOP and I have an object. If I:
var_dump($obj);
I get:
object(stdClass)[55]
public 'date' => int 1295297161
public 'id' => int 11
How can I retrieve the "55"?
As others have mentioned you can't read it, without parsing the output of var_dump. But if all you are looking for is a way to uniquely identify an object, then you should use spl_object_hash.

Categories