我有一个 codeigniter 应用程序,它查询一个 api 并获取一个 simplexml 对象。我从 simplexml 对象中硬编码了我想要的值,但后来发现对象的结构并不总是相同的,所以我不能使用强制转换来获得我需要的值。
是否有一个类似 array_search 的函数可以用于此,所以我可以使用“classid”来获取值?以下是 simplexml 对象的示例:
[Package] => SimpleXMLElement Object
(
[@attributes] => Array
(
[ID] => e981b9a72cd305c7d97cc530ef9b3015
)
[Postage] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[CLASSID] => 3
)
[MailService] => Express Mail<sup>®</sup>
[Rate] => 57.60
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[CLASSID] => 2
)
[MailService] => Express Mail<sup>®</sup> Hold For Pickup
[Rate] => 57.60
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[CLASSID] => 55
)
[MailService] => Express Mail<sup>®</sup> Flat Rate Boxes
[Rate] => 39.95
)
我正在使用它来获取 [Rate] 值:
$usps->Package->Postage[0]->Rate
问题是我需要使用 [CLASSID] 因为这与数字键(?)中的数据发生变化的地方是一致的。