#1
  1. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,233
    Rep Power
    593

    Object Reference Help


    I'm trying to do a little reverse engineering but I cannot seem to get the right syntax for the value I want. A var_dump($results) shows the following:
    Code:
    stdClass::__set_state(array(
       'row' =>
      array (
        'vproduct_id' => '54',
      ),
       'rows' =>
      array (
        0 =>
        array (
          'vproduct_id' => '54',
        ),
      ),
       'num_rows' => 1,
    ))
    I want to get the vproduct_id but this doesn't work:
    PHP Code:
    print($results->row[0]->vproduct_id); 
    I've not seen an object dumped quite this way before (stdClass::__set_state) so I would guess I need to do something less trivial to get that value. Can someone help? TIA.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    501
    Rep Power
    127
    That is an odd output.

    Try this, though:
    PHP Code:
    print $results->row['vproduct_id']; 
    The row element isn't an array, so row[0] isn't applicable.

    or:

    PHP Code:
    print $results->rows[0]['vproduct_id']; 
    rows[0] is shown as an array in the dump, not an object.
    LinkedIn: Dave Mittner
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    101
    Rep Power
    26
    That __set_state() magic method was a new one for me so I thought I'd dump a couple search results here.
    PHP doc: http://www.php.net/manual/en/languag...ject.set-state
    Some person's writeup: http://www.thoughtlabs.com/2008/02/0..._state-method/
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,233
    Rep Power
    593
    Thanks for the replies. Dave's suggestion was correct. I guess I was over-complicating it.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,760
    Rep Power
    9397
    That output is from var_export, not var_dump().

    I would expect
    PHP Code:
    $results->row->vproduct_id // or
    $results->row["vproduct_id"]

    $results->rows[0]->vproduct_id // or
    $results->rows[0]["vproduct_id"

IMN logo majestic logo threadwatch logo seochat tools logo