23

i have:

stdClass Object
(
    [0] => stdClass Object
        (
            [one] => aaa
            [two] => sss
        )

    [1] => stdClass Object
        (
            [one] => ddd
            [two] => fff
        )

    [2] => stdClass Object
        (
            [one] => ggg
            [two] => hhh
        )
}

and i must get this with keys, for example:

$var = $stdClass[0]; 

but i have error:

Fatal error: Cannot use object of type stdClass as array in

Is possible parse this stdClass to array and use this with keys?

3

11 Answers 11

69

Cast it to an array:

$array = (array)$stdClass;
5
  • Where does that data come from to begin with?
    – deceze
    Jul 9, 2012 at 13:43
  • 1
    i use json and json_decode function. I add for this function parameter true, but still i have empty array Jul 9, 2012 at 13:45
  • 4
    $array = json_decode($jsonData, true); does not work? Please show a full example, including what the JSON data looks like.
    – deceze
    Jul 9, 2012 at 13:48
  • if i use true then i have empty array. If i use yout solution this working only for current array - children array still are stdClass Jul 9, 2012 at 13:52
  • Yes, the children will still be stdClass objects, you'll have to cast them as well.
    – deceze
    Jul 9, 2012 at 13:56
36

If you're using json_decode to convert that JSON string into an object, you can use the second parameter json_decode($string, true) and that will convert the object to an associative array.

If not, what everybody else has said and just type cast it

$array = (array) $stdClass;

1
  • 2
    You should have no problem using json_decode is you do: $array = json_decode($string, true); Then using print_r will give the associative array with the keys. Jul 9, 2012 at 13:46
8

Your problem is probably solved since asking, but for reference, quick uncle-google answer:

function objectToArray($d) {
  if(is_object($d)) {
    $d = get_object_vars($d);
  }
  if(is_array($d)) {
    return array_map(__FUNCTION__, $d); // recursive
  } else {
    return $d;
  }
}

Full article here. Note I'm not associated with the original author in any way.

0
7

Cast it

$array = (array) $stdObject;
3
  • if now user $array[0] then i have empty array Jul 9, 2012 at 13:40
  • 2
    this working only for current array. Childrens array still are stdObject Jul 9, 2012 at 13:52
  • @LucciFangorci You didn't mentioned, that you want the whole tree. For now I don't even see a reason, why: echo $array[0]->two.
    – KingCrunch
    Jul 9, 2012 at 13:59
4

Of course you can typecast, $var = (array) $obj;, but I would suggest ArrayAccess to your class.

By using ArrayAccess, you can then treat your objects and data as if it was an array, or natively as an object.

3

Cast it into an array. Currently it is not readable to PHP as an array.

$array = (array)$stdClass;
1
  • $array[0][one] will return aaa
    – Max Hudson
    Jul 9, 2012 at 13:55
3

Essentially, just type cast it:

$arr = (array)$obj;
$var = $arr[0];

But read the caveats here.

1
  • since you've got objects inside of objects, you'll need to cast the containing object to an array and iterate it. While iterating through it, access the property you want $var->one.
    – dnagirl
    Jul 9, 2012 at 13:58
2

If you have an nested array you can used json_encode and json_decode to convert the whole object to an array:

$result = json_decode(json_encode($source), JSON_OBJECT_AS_ARRAY);
1

This one worked for me, The decoding and encoding makes for a regular array

$array = json_decode(json_encode($object), True);

0
function load_something () : \stdClass {

    $result = new \stdClass();

    $result->varA   = 'this is the value of varA';
    $result->varB   = 'this is the value of varB';
    $result->varC   = 'this is the value of varC';

    return $result;
}

$result = load_something();

echo ($result instanceof stdClass)?'Object is stdClass':'Object is not stdClass';
echo PHP_EOL;

print_r($result);

//directly extract a variable from stdClass 
echo PHP_EOL . 'varA = ' . ($result->varA);

//convert to array, then extract
$array = (array)$result;
echo PHP_EOL . 'varA = ' . $array['varA'];
-3

stdClass is an object so u can access value from it like

echo stdClass->one;
1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.