jaybo Posted September 23, 2021 Share Posted September 23, 2021 I have a stdClass Object that I need to then point to a single value within it. Here is the object returned saved in variable named $response: stdClass Object ( [data] => stdClass Object ( [result] => Array ( [0] => stdClass Object ( [label] => FC#### **[value] => 72144** ) [1] => stdClass Object ( [label] => SERVICE [value] => 72123 ) [2] => stdClass Object ( [label] => all_ssid [value] => all_ssid ) ) [defaultValue] => all_ssid ) [msg] => [retCode] => 0 ) How do I then point to [value] => 72144 I understand that the path is $response->data->result->0->value but don’t know how what to use for the “0” pointing to the 1st array result. I have tried $response->data->result->{‘value’} which returns " Warning: Attempt to read property “label” on array". Quote Link to comment https://forums.phpfreaks.com/topic/313796-oop-point-to-array-value-inside-of-a-stdclass-object/ Share on other sites More sharing options...
ginerjm Posted September 23, 2021 Share Posted September 23, 2021 IIRC - $response['data']['result'][0]['value'] Pretty simple, eh? Quote Link to comment https://forums.phpfreaks.com/topic/313796-oop-point-to-array-value-inside-of-a-stdclass-object/#findComment-1590266 Share on other sites More sharing options...
Barand Posted September 23, 2021 Share Posted September 23, 2021 1 hour ago, jaybo said: what to use for the “0” pointing to the 1st array result What's wrong with using "0"? Quote Link to comment https://forums.phpfreaks.com/topic/313796-oop-point-to-array-value-inside-of-a-stdclass-object/#findComment-1590267 Share on other sites More sharing options...
Barand Posted September 23, 2021 Share Posted September 23, 2021 11 minutes ago, ginerjm said: Pretty simple, eh? Agreed. You do realize that it's objects and not arrays? Quote Link to comment https://forums.phpfreaks.com/topic/313796-oop-point-to-array-value-inside-of-a-stdclass-object/#findComment-1590268 Share on other sites More sharing options...
ginerjm Posted September 23, 2021 Share Posted September 23, 2021 Did I do it incorrectly? Don't use them much at all but I thought they still read the same Quote Link to comment https://forums.phpfreaks.com/topic/313796-oop-point-to-array-value-inside-of-a-stdclass-object/#findComment-1590269 Share on other sites More sharing options...
Solution Barand Posted September 23, 2021 Solution Share Posted September 23, 2021 -> for object properties [] for array keys So $response->data->result[0]->value Quote Link to comment https://forums.phpfreaks.com/topic/313796-oop-point-to-array-value-inside-of-a-stdclass-object/#findComment-1590270 Share on other sites More sharing options...
ginerjm Posted September 23, 2021 Share Posted September 23, 2021 Aha! Like I said (as in 'IIRC') my syntax was showing a bit of rust. Quote Link to comment https://forums.phpfreaks.com/topic/313796-oop-point-to-array-value-inside-of-a-stdclass-object/#findComment-1590271 Share on other sites More sharing options...
jaybo Posted September 23, 2021 Author Share Posted September 23, 2021 Makes complete sense - thank you Barand. Thank you for your input also ginerjm. Quote Link to comment https://forums.phpfreaks.com/topic/313796-oop-point-to-array-value-inside-of-a-stdclass-object/#findComment-1590273 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.