-1

我创建了一个发送 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
                    )

            )

    )

)

在此响应中循环遍历每个单独项目的最佳和最快方法是什么?谢谢

4

2 回答 2

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);
于 2013-05-08T08:55:16.843 回答
0

在数组上使用foreach循环:

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

另请参阅:stdClass 对象和 foreach 循环

于 2013-05-08T08:53:50.677 回答