1

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?

Gordon
  • 312,688
  • 75
  • 539
  • 559
Farhan Ahmad
  • 5,148
  • 6
  • 40
  • 69
  • 3
    Why not alias the column in the query, i.e., `MAX(sort_order) as MAX_SORT_ORDER`. – mellamokb Nov 08 '12 at 00:09
  • Yes, I did that and it worked but I was just wondering how one would reference an object in this situation? It was the first time I saw something like this. – Farhan Ahmad Nov 08 '12 at 00:12
  • 2
    Curly brackets should do the job, too: `$object->{'MAX(sort_order)'};`. Just FYI. Standard PHP syntax. See as well http://cowburn.info/2008/01/12/php-vars-curly-braces/ – hakre Nov 08 '12 at 01:05

1 Answers1

3

Try add this in your query MAX(sort_order) AS max_sort_order in your query

Ariaan
  • 1,105
  • 1
  • 9
  • 13
  • Yes, I did that and it worked but I was just wondering how one would reference an object in this situation? It was the first time I saw something like this. – Farhan Ahmad Nov 08 '12 at 00:13
  • 1
    In PHP the other to options would be to fetch the query as a numeric array or a associated array, so you could read it as `$result[3];` or `$result['MAX(sort_order)'];` It would be useful to know which method you used to execute the query. – Ariaan Nov 08 '12 at 00:15
  • Hmmm I guess that would work also. Thanks :) – Farhan Ahmad Nov 08 '12 at 00:30