如何从php中的simplexml_load_file()获取值?

时间:2015-03-27 04:31:37

标签: php arrays xml object

我从api获得XML响应。这是代码

$xml = simplexml_load_file($actualurl);
    echo '<pre>';
print_r($xml);
    echo '</pre>';

输出将如下所示

SimpleXMLElement Object
(
    [MERCHANT] => SimpleXMLElement Object
        (
            [RESPONSE] => SimpleXMLElement Object
                (
                    [url] => https://xxx/xxx/epi/fts
                    [param] => Array
                        (
                            [0] => NBFundTransfer
                            [1] => 354206
                            [2] => fhyF1K5nmhXJmhoIUb04%2BEiIe80rLda52l4bCviNPwg%3D
                            [3] => 1
                        )

                )

        )

)

问题我无法获得param数组的节点值。我尝试了$xml->param,但它没有显示任何返回空值。

2 个答案:

答案 0 :(得分:3)

使用

print_r($xml->MERCHANT->RESPONSE->param[0]);

答案 1 :(得分:0)

    print_r($xml->MERCHANT->RESPONSE->param);

    $param = $xml->MERCHANT->RESPONSE->param;
    echo $param[0]; //=> NBFundTransfer
 echo '<br>';
    echo $param[1]; // => 354206
 echo '<br>';
    echo $param[2]; // => fhyF1K5nmhXJmhoIUb04%2BEiIe80rLda52l4bCviNPwg%3D
 echo '<br>';
    echo $param[3]; // => 1