解析Web服务对象

时间:2014-07-31 17:40:29

标签: php web-services oop

我有一个返回以下

的PHP Web服务
$response = $client->submit($requestParams);

stdClass Object
(
    [return] => stdClass Object
        (
            [result_code] => 0
            [result_data] => City=Chicago
            [message_text] => 
        )

)

我想要的不是将[result_data] => City=Chicago分配给像

这样的变量

$city = [result_data] => City=Chicago以便

$city = Chicago;

所以要清楚我是否打印了一个print_r($ response)

stdClass Object
(
    [return] => stdClass Object
        (
            [result_code] => 0
            [result_data] => City=Chicago
            [message_text] => 
        )

)

从这个我只想要在这个例子中[result_data]的值,该值将是字符串" City = Chicago"

2 个答案:

答案 0 :(得分:1)

如果City=Chicago是一个字符串,您可以这样做:

$response = new stdClass(); // this is a sample object taking your example

$response->result_code = 0;
$response->result_data = 'City=Chicago';
$response->message_text = '';

$result = explode('=', $response->result_data);

${$result[0]} = $result[1]; // or $City = $result[1];
echo $City;

希望这适合你:)

答案 1 :(得分:0)

这是一种方法 -

$arrRD = explode('=',$response->return->result_data); // separates the City=Chicago string into an array
$city = $arrRD[1]; // 'Chicago' is the second part of the array

编辑:PhpFiddle - http://phpfiddle.org/main/code/8ut6-mtg3