5

How can I access the property/ value of an array which has been converted into an object? For instance, I want to access the value in the index 0,

$obj = (object) array('qualitypoint', 'technologies', 'India');
var_dump($obj->0);

error,

Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$' in C:...converting_to_object.php on line 11

Run
  • 54,938
  • 169
  • 450
  • 748
  • 1
    @MakuraYami if you had tried this, you'd see that it wouldn't work. `$obj` is not an array and you'd get an error message to that extent – Aleks G Apr 04 '12 at 14:33
  • What's interesting is that `var_dump(get_class_vars($obj))` prints an empty array. – Aleks G Apr 04 '12 at 14:34
  • That's why i sayd "I think" and i commented it instaed of posting an awnser. Anyways look at this: http://www.richardcastera.com/blog/php-convert-array-to-object-with-stdclass – MakuraYami Apr 04 '12 at 14:35
  • Can I ask for some background info? Why has the array has been converted into an object? – Squig Apr 04 '12 at 14:42

2 Answers2

4

Trying this:

$obj = (object) array('test' => 'qualitypoint', 'technologies', 'India');

var_dump($obj->test);

The result is:

string(12) "qualitypoint"

But trying to access $obj->0, the same error shows up: Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$'

If you loop through the object, tough, you can access the properties normally as an usual array:

foreach($obj as $x) {
    var_dump($x);
}

Apperantly, the property naming rules are the same as the basic variable naming rules.

If you convert it to an ArrayObject instead, you can access the index normally:

$obj = new ArrayObject(array('qualitypoint', 'technologies', 'India'));

And dumping it:

var_dump($obj[0]);

You would get:

string(12) "qualitypoint"
Daniel Ribeiro
  • 10,156
  • 12
  • 47
  • 79
2

The reason you can not access values via $obj->0 its because it against PHP variable naming see http://php.net/manual/en/language.variables.basics.php for more information. even if you use ArrayObject you would still have the same issues

but there is a patch to this ... you can convert all integer keys to string or write your own conversion function

Example

$array  = array('qualitypoint', 'technologies', 'India' , array("hello","world"));
$obj = (object) $array;
$obj2 = arrayObject($array);
function arrayObject($array)
{
    $object = new stdClass();
    foreach($array as $key => $value)
    {
        $key = (string) $key ;
        $object->$key = is_array($value) ? arrayObject($value) : $value ;
    }
    return $object ;
}
var_dump($obj2->{0}); // Sample Output
var_dump($obj,$obj2); // Full Output to see the difference 


$sumObject = $obj2->{3} ; /// Get Sub Object
var_dump($sumObject->{1});  // Output world

Output

   string 'qualitypoint' (length=12)

Full output

object(stdClass)[1]
  string 'qualitypoint' (length=12)
  string 'technologies' (length=12)
  string 'India' (length=5)

    array
      0 => string 'hello' (length=5)
      1 => string 'world' (length=5)

object(stdClass)[2]
  public '0' => string 'qualitypoint' (length=12)
  public '1' => string 'technologies' (length=12)
  public '2' => string 'India' (length=5)
  public '3' => 
    object(stdClass)[3]
      public '0' => string 'hello' (length=5)
      public '1' => string 'world' (length=5)

Multi Array Outpur

Thanks

:)

Baba
  • 94,024
  • 28
  • 166
  • 217