从 api xml 格式响应 return 数组中获取值

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 here 的 xml 格式响应。现在,正如您在 api 响应中看到的那样(检查附件 link),我正在获取 alltimeuptimeratio 的值,然后将其放入数组中,然后将其作为 json_encode 回显。 (参考下面的代码)

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

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

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

我的问题是,为什么 alltimeratio1 是“:{"0":"99.73}"?如果我尝试回显 $alltimeratio1,我会得到没有 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 解析 return 响应,那么您可以通过识别位于您的 alltimeratio 数组位置的第二个数组键来访问该值。例如。

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

或简称为

var theValueOfTheAllTimeRatio = thearray[2];