Robert Morel Robert Morel - 1 month ago 7
PHP Question

Access a Multiple level stdClass object field?

I have received this ridiculously complex array of objects as a SOAP response. I need to print out the "string_operator_collection" using a loop. It is a collection of operators similiar to ["equal_wild"] but with all different names. The nesting is so deep that I can't seem to access using normal methods. Any ideas please?

object(stdClass)#2 (1) {
["get_search_frame_details_response"]
=> object(stdClass)#3 (2) {
["response_context"]=> object(stdClass)#4 (3) {
["session_token"]
=> string(28) "b0ac045931183d1ead6b9f6da061"
["response_status"]=> string(2) "OK"
["response_message"]=> string(0) "" }
["search_frame_details_response_data"]
=> object(stdClass)#5 (2) {
["search_field_collection"]=> object(stdClass)#6 (1) {
["search_field"]=> array(4) {
[0]=> object(stdClass)#7 (3) {
["field_id"]
=> string(82)
"OsCashAllocationCashAllocationPk"
["field_name"]=> string(15) "Customer Number"
["field_operator"]
=> object(stdClass)#8 (1) {
["string_operator_collection"]
=> object(stdClass)#9 (16) {
["equal_wild"]=> string(2) "=*"


My best effort so far is:

get_search_frame_details_response->
search_frame_details_response_data->
search_field_collection->search_field;

print_r(array_values($search_frame_op1));

Answer

I think (display is not helpful) the address of string_operator_collection is

$obj
->get_search_frame_details_response
->search_frame_details_response_data
->search_field_collection
->search_field[]
->field_operator
->string_operator_collection
->equal_wild

So as the array starts at

$obj
->get_search_frame_details_response
->search_frame_details_response_data
->search_field_collection
->search_field[];` 

we can write a loop like this

$the_array =  $obj
                ->get_search_frame_details_response
                ->search_frame_details_response_data
                ->search_field_collection
                ->search_field   

foreach($the_array as $field ) {
    echo $field
            ->field_operator
            ->string_operator_collection
            ->equal_wild;
}