Cake PHP,从xml获取价值

时间:2022-10-24 20:48:30

I would like to get specific value from XML data in Cake Php. This is what I've got so far:

我想从Cake Php中获取XML数据的具体价值。这是我到目前为止所得到的:

after doing print_r($output) in controller this is what I've got;

在控制器中执行print_r($ output)后,这就是我所拥有的;

<?xml version="1.0" encoding="UTF-8"?>
<response>
  <xmlArray>
    <numbers>52619657</numbers>
  </xmlArray>
</response>

Because I don't know how to get it directly from xml, so I convert it to array in controller as cakePHP doc mention.

因为我不知道如何从xml直接获取它,所以我将它转换为控制器中的数组,如cakePHP doc提到的那样。

$xmlArray = Xml::toArray(Xml::build($out));

The result from print_r($xmlArray); is

print_r($ xmlArray)的结果;是

Array
(
    [response] => Array
        (
            [xmlArray] => Array
                (
                    [numbers] => 52619657
                )
        )
)

I tried to get the numbers '52619657'. So my attempt is

我试图得到数字'52619657'。所以我的尝试是

print_r ($xmlArray['numbers']);

But it doesn't work (error is Undefined index:). So I try to use IN method like here, but actually I don't know how to do it. How do I get the number '52619657'? in cake PHP.

但它不起作用(错误是未定义的索引:)。所以我尝试使用像这里的IN方法,但实际上我不知道该怎么做。我怎么得到号码'52619657'?在蛋糕PHP。

Thank you so much

非常感谢

1 个解决方案

#1


2  

I don't see why you would need a 3rd party library, like Cake's XML class for this. You can do this easily with SimpleXML:

我不明白你为什么需要第三方库,比如Cake的XML类。您可以使用SimpleXML轻松完成此操作:

$response = simplexml_load_string($output);
echo $response->xmlArray->numbers;

Your approach doesn't work because your $xmlArray is a nested array and doing $xmlArray['numbers'] will try to fetch numbers from the first level. You would need to use $xmlArray['response']['xmlArray']['numbers']; Knowing how to read an array is basic knowledge, so consider rereading this part in the PHP manual.

你的方法不起作用,因为你的$ xmlArray是一个嵌套数组,而$ xmlArray ['numbers']会尝试从第一级获取数字。你需要使用$ xmlArray ['response'] ['xmlArray'] ['numbers'];知道如何读取数组是基本知识,因此请考虑在PHP手册中重读这一部分。

#1


2  

I don't see why you would need a 3rd party library, like Cake's XML class for this. You can do this easily with SimpleXML:

我不明白你为什么需要第三方库,比如Cake的XML类。您可以使用SimpleXML轻松完成此操作:

$response = simplexml_load_string($output);
echo $response->xmlArray->numbers;

Your approach doesn't work because your $xmlArray is a nested array and doing $xmlArray['numbers'] will try to fetch numbers from the first level. You would need to use $xmlArray['response']['xmlArray']['numbers']; Knowing how to read an array is basic knowledge, so consider rereading this part in the PHP manual.

你的方法不起作用,因为你的$ xmlArray是一个嵌套数组,而$ xmlArray ['numbers']会尝试从第一级获取数字。你需要使用$ xmlArray ['response'] ['xmlArray'] ['numbers'];知道如何读取数组是基本知识,因此请考虑在PHP手册中重读这一部分。