我有一个由json_decode()生成的数组。$array_data = json_decode(json_encode(simplexml_load_string($data)), true);
输出数组如下:
Array
(
[@attributes] => Array
(
[version] => 1.0
)
[response] => Array
(
[operation] => Array
(
[@attributes] => Array
(
[name] => ADD_REQUEST
)
[result] => Array
(
[statuscode] => 200
[status] => Success
[message] => Request added successfully
)
[Details] => Array
(
[0] => Array
(
[workorderid] => 291885
)
[1] => Array
(
[parameter] => Array
(
[name] => workorderid
[value] => 291885
)
)
)
)
)
)
我需要在另一个php变量中保存键'workorderid'的值,以便我可以在代码中进一步使用它。取值为dynamic
我现在一直在挣扎,寻找一些指导。有人能帮我们把这件事做完吗?提前感谢!
问候,Pooja
如果您确定Details
下的第一个数组将包含workorderid
键,您可以直接访问它:
$workorderid = $array_data['response']['operation']['Details'][0]['workorderid'];
var_dump($workorderid);
输出:字符串(6)"291885"
如果你不知道它在Details
下的哪个数组中,你必须遍历它并找到它:
$workorderid = null;
foreach ($array_data['response']['operation']['Details'] as $detail) {
if (isset($detail['workorderid'])) {
$workorderid = $detail['workorderid'];
break;
}
}
if (null !== $workorderid) {
var_dump($workorderid);
}
输出:字符串(6)"291885"
如果您只需要从响应中获取一个键,这是一个可行的解决方案。如果您需要更多的键,我建议将响应数据映射到更可读的结构中。