Page 1 of 1

Help referencing object properties

Posted: Thu Apr 05, 2012 7:25 pm
by flying_circus
This is kind of a new one for me, how do I fetch a property of an object when the key contains a space?

// Notice the space in 'Panel 1'
Example: $data->design->colors->Panel 1->color;

Code: Select all

<?php
  foreach( $data->design->colors as $key => $value ) {
    print $key . "\n";
  }
?>

/* Output */
Center Flap D
Reserve Handle
Panel 3
Pinstripe D-E
Center Flap B
Cutaway Handle
Center Flap E
Center Flap C
Panel 2
Party Pinstripe B
Party Stripe
Panel 1
Legpad (Out)
Pinstripe C-D
Panel 4
Harness
Backpad
Center Flap A
Pinstripe A-B
Ring Cover
Party Pinstripe A
Pinstripe B-C
Legpad (In)
Main Risers
Binding Tape
Panel 5

Re: Help referencing object properties

Posted: Thu Apr 05, 2012 10:17 pm
by requinix
Are you sure $data->design->colors is an object and not an array? Does array syntax work?

Otherwise,

Code: Select all

$data->design->colors->{"Panel 1"}->color

Re: Help referencing object properties

Posted: Sun Apr 08, 2012 11:43 am
by flying_circus
requinix wrote:Are you sure $data->design->colors is an object and not an array? Does array syntax work?

Otherwise,

Code: Select all

$data->design->colors->{"Panel 1"}->color
Correct, it is definitely a stdClass object, array syntax does not work. I will try your suggestion above, I am interested to see if it works.

It should not be valid to have spaces in variable or method names, but this is a weird situation created by decoding a JSON object that was encoded in actionscript

Re: Help referencing object properties

Posted: Sun Apr 08, 2012 4:46 pm
by requinix
flying_circus wrote:but this is a weird situation created by decoding a JSON object that was encoded in actionscript
If you're using json_decode() you can have it return an array instead of an object.

Re: Help referencing object properties

Posted: Mon Apr 09, 2012 12:30 am
by flying_circus
requinix wrote:
flying_circus wrote:but this is a weird situation created by decoding a JSON object that was encoded in actionscript
If you're using json_decode() you can have it return an array instead of an object.
Hey, I really appreciate your feedback, you taught me something new. Between this and the last post, I should be able to solve my problem.