循环SOAP响应

时间:2013-05-08 08:45:05

标签: php loops soap

我创建了一个发送SOAP请求并获得响应的应用程序。这工作正常,但我无法循环遍历所有单个结果,以便将它们组织成一个数组。以下是回复:

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
                    )

            )

    )

在此响应中循环浏览每个项目的最佳和最快捷方式是什么? 感谢

2 个答案:

答案 0 :(得分:1)

假设此对象存储在$ ret变量中,并且您希望将单个结果存储在数组$ data中。

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

get_object_vars 将对象及其值中的所有变量转储为数组

在foreach循环中,您可以访问,打印或存储项目数据,如下所示:

echo $item->email;
$test = $item->GYMNAME;
$data[] = array($item->email, $item->GYMNAME);

答案 1 :(得分:0)

在阵列上使用foreach循环:

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

另请参阅:stdClass object and foreach loops