9

Hopefully a very easy question for you. I have a PHP object, one of the properties has brackets in it (from using MIN mysql command):

stdClass Object ( [uid] => 5 [min(time)] => 13.40 )

how can I call this property? I have tried all sorts but nothing seems to work, and cannot seem to find any info on the interwebs.

Thanks very muchly!

Chris
  • 1,557
  • 5
  • 19
  • 36
  • Related: [How do I access a PHP object attribute having a dollar sign?](http://stackoverflow.com/q/2093169/367456) – hakre Dec 31 '13 at 20:39
  • Related: [How do I access this object property with a hyphenated name?](http://stackoverflow.com/q/758449/367456) – hakre Dec 31 '13 at 21:00

2 Answers2

12

To directly answer your question, use curly braces around the property name as a string:

$row->{'min(time)'}

A better idea, though, is to give your aggregate value an alias in your SQL as mentioned by the other answers, then access the property by that alias. Defining an alias also gives you an opportunity to supply a more meaningful name than just a call to some aggregate function on a certain column or value.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
10

you could modify your select statement to give the field an alias like:

ex. SELECT min(time) as min_time....

then your returned object should have this indexed like so $obj->min_time.

i hope this helps.

Robert Van Sant
  • 1,497
  • 10
  • 12