从api xml格式的响应返回数组中获取值


getting the value from a api xml format response return array

我通过.解析API的xml响应

$responseXML2 = curl_seasson($url['phemail']);
$xml2 = simplexml_load_string($responseXML2);

从xml 中获取值

$alltimeratio1 = $xml2->monitor['alltimeuptimeratio'];

您可以在这里查看api的xml格式响应。现在,正如您在api响应中看到的那样(检查附加链接),我获得了alltimeuptimeratio的值,然后将其放入一个数组中,然后将它作为jsonencode进行回显。(参考下面的代码)

$array = array("ph" => array("phweb" => $status, "phemail" => $status2, "alltimeratio1" => $alltimeratio1));
echo json_encode($array);

假设我已经在上面的数组中存储了$status和$status2内容,当我在浏览器中运行它时,我会得到什么。(参考下面的代码)

{"ph":{"phweb":"boxup","phemail":"boxup","alltimeratio1":{"0":"99.73"}}}

我的问题是,为什么alltimeratio1是":{"0:"99.73}"?如果我尝试呼应$alltimeratio 1,我得到的是没有0的99.73。有没有办法在数组集中只存储99.73(没有0)?

我希望阵列结构的结果是这样的

{"ph":{"phweb":"boxup","phemail":"boxup","alltimeratio1":"99.73"}}

任何帮助、想法、线索、建议和建议都将不胜感激。

我不知道您是如何解析XML响应的,但它似乎被视为一个数组。这就是你得到这个{"0":"99.73"} 的原因

我尝试使用一个简单的脚本,并且运行良好,所以我认为问题在于解析XML。

$alltimeratio1 = "99.73";
$status = $status2 = "boxup";
$array = array("ph" => array("phweb" => $status, "phemail" => $status2, "alltimeratio1" => $alltimeratio1));
echo json_encode($array);

响应:

 {"ph":{"phweb":"boxup","phemail":"boxup","alltimeratio1":"99.73"}}

如果您使用ajax解析返回响应,那么您可以通过识别alltimeratio数组中的第二个数组键来访问该值。例如

var thearray = new Array(result["ph"]["phweb"], result["ph"]["phemail"], result["ph"]["alltimeratio1"][0]);

或者简单地称为

var theValueOfTheAllTimeRatio = thearray[2];