1

I have a stdClass, that has an attribute with a dot in it's name. It's returned to me via an API, so there's nothing I can do with the name. It looks like this:

stdClass Object ( [GetPlayerResult] => stdClass Object ( [RegistrationResponses.Player] => Array (...)

Now, how do I access the array? When I do this:

print_r($result->GetPlayerResult->RegistrationResponses.Player);

it (obviously) prints just "Player". I have tried putting apostrophes around the last part, using [''] syntax (like an associative array), none of that works and throws a 500 error. Any ideas? Thanks!

Marek Buchtela
  • 973
  • 3
  • 19
  • 42

2 Answers2

5

You can try using braces:

$object->GetPlayerResult->{"RegistrationResponses.Player"}

Or you can cast it to an associative array

$result = (array) $object->GetPlayerResult;
$player = $result["RegistrationResponses.Player"];

If you are parsing your website response using json_decode, note the existence of the second parameter to return as associative array:

assoc

When TRUE, returned objects will be converted into associative arrays.

SOFe
  • 7,867
  • 4
  • 33
  • 61
-2

Please convert your object to array by using this function

$arr = (array) $obj;
echo "<pre>";print_r($arr);exit;

by print $arr you can show elements of array.

Darshan ambaliya
  • 301
  • 3
  • 20
  • 2
    You can't _access_ an element of an array using print_r... You only see it from the output, but not actually "access" it by reading it into a useful value. And I don't think printing an associative array has any obvious advantages over printing an object... Moreover, you are only casting the shallow object into an array, but the deep objects are still objects. – SOFe Dec 15 '16 at 13:01
  • ok i just want to convey that you can see all the elements nothing else. – Darshan ambaliya Dec 15 '16 at 13:06
  • 1
    You can do exactly the same thing without converting it to an array. Especially when you don't really deeply convert it into an array. – SOFe Dec 15 '16 at 13:09