-1

I have created an application that sends out a SOAP request and gets a response back. This is working fine but I am having trouble looping through all the individual results in order to organize them into an array. Here is the response:

stdClass Object
    (
    [FITgymlistResult] => stdClass Object
        (
        [FITgym] => Array
            (
                [0] => stdClass Object
                    (
                        [GYMGUID] => 45124542-bca5-e211-8f4a-00155d007722
                        [GYMNAME] => Belfast
                        [Postcode] => 
                        [Phone] => 
                        [Email] => belfast@fitspace.co.uk
                    )

                [1] => stdClass Object
                    (
                        [GYMGUID] => aece7776-bca5-e211-8f4a-00155d007722
                        [GYMNAME] => Bournemouth
                        [Addressline1] => St Paul's Road
                        [Postcode] => 
                        [Phone] => 
                        [Email] => Bournemouth@fitspace.co.uk
                    )

                [2] => stdClass Object
                    (
                        [GYMGUID] => 8eaa258e-bca5-e211-8f4a-00155d007722
                        [GYMNAME] => Bradford
                        [Postcode] => 
                        [Phone] => 
                        [Email] => Bradford@fitspace.co.uk
                    )

                [3] => stdClass Object
                    (
                        [GYMGUID] => 935bfdca-bca5-e211-8f4a-00155d007722
                        [GYMNAME] => Islington
                        [Postcode] => 
                        [Phone] => 
                        [Email] => holloway@fitspace.co.uk
                    )

                [4] => stdClass Object
                    (
                        [GYMGUID] => fe104008-bda5-e211-8f4a-00155d007722
                        [GYMNAME] => Lincoln
                        [Postcode] => 
                        [Phone] => 
                        [Email] => lincoln@fitspace.co.uk
                    )

                [5] => stdClass Object
                    (
                        [GYMGUID] => ff3cd339-bda5-e211-8f4a-00155d007722
                        [GYMNAME] => Mitcham
                        [Postcode] => 
                        [Phone] => 
                        [Email] => Mitcham@fitspace.co.uk
                    )

                [6] => stdClass Object
                    (
                        [GYMGUID] => 496e8149-bda5-e211-8f4a-00155d007722
                        [GYMNAME] => Nottingham
                        [Postcode] => 
                        [Phone] => 
                        [Email] => Nottingham@fitspace.co.uk
                    )

                [7] => stdClass Object
                    (
                        [GYMGUID] => 48f26656-bda5-e211-8f4a-00155d007722
                        [GYMNAME] => Sheffield
                        [Postcode] => 
                        [Phone] => 
                        [Email] => Sheffield@fitspace.co.uk
                    )

                [8] => stdClass Object
                    (
                        [GYMGUID] => 1c136968-bda5-e211-8f4a-00155d007722
                        [GYMNAME] => Woolwich
                        [Postcode] => 
                        [Phone] => 
                        [Email] => Woolwich@fitspace.co.uk
                    )

            )

    )

)

What would be the best and quickest way to loop through each individual item in this response? Thanks

James
  • 2,800
  • 7
  • 45
  • 81

2 Answers2

1

Let say that this object is stored in $ret variable and you want to store individual results in array $data.

$data = array();
foreach($ret->FITgymlistResult->FITgym as $item)
{
    $data[] = get_object_vars($item);
}

get_object_vars dumps as array all variables from the object and theirs values

also

in foreach loop you can access, print or store item data like this:

echo $item->email;
$test = $item->GYMNAME;
$data[] = array($item->email, $item->GYMNAME);
Landon
  • 256
  • 2
  • 6
  • Thanks. I don't why I was having such an issue with it. Probably too early in the morning! :) – James May 08 '13 at 09:02
0

Use a foreach loop on the array:

foreach( $response->FITgymlistResult->FITgym as $row )
{
    var_dump( $row->GYMGUID, $row->GYMNAME );
}

See also: stdClass object and foreach loops

Community
  • 1
  • 1
feeela
  • 29,399
  • 7
  • 59
  • 71