我使用的API返回一个对象,然后我将其变成一个数组,以便在我的API中使用。但我似乎无法使用名称上带有* _的键来选择值。我不知道为什么。以下是数组的外观:
Array
(
[*_apiKey] => XXXXXXXXXXXXX
[*_values] => Array
(
[carrier] => XXXXXXXXXXXXX
[tracking_number] => XXXXXXXXXXXXX
[address_from] => Shippo_Object Object
(
[_apiKey:protected] => XXXXXXXXXXXXX
[_values:protected] => Array
(
[city] => XXXXXXXXXXXXX
[state] =>
[zip] => XXXXXXXXXXXXX
[country] => XXXXXXXXXXXXX
)
[_unsavedValues:protected] => Shippo_Util_Set Object
(
[_elts:Shippo_Util_Set:private] => Array
(
)
)
[_transientValues:protected] => Shippo_Util_Set Object
(
[_elts:Shippo_Util_Set:private] => Array
(
)
)
[_retrieveOptions:protected] => Array
(
)
)
[address_to] => Shippo_Object Object
(
[_apiKey:protected] => XXXXXXXXXXXXX
[_values:protected] => Array
(
[city] => XXXXXXXXXXXXX
[state] =>
[zip] => XXXXXXXXXXXXX
[country] => XXXXXXXXXXXXX
)
[_unsavedValues:protected] => Shippo_Util_Set Object
(
[_elts:Shippo_Util_Set:private] => Array
(
)
)
[_transientValues:protected] => Shippo_Util_Set Object
(
[_elts:Shippo_Util_Set:private] => Array
(
)
)
[_retrieveOptions:protected] => Array
(
)
)
[eta] =>
[servicelevel] => Shippo_Object Object
(
[_apiKey:protected] => XXXXXXXXXXXXX
[_values:protected] => Array
(
[token] =>
[name] =>
)
[_unsavedValues:protected] => Shippo_Util_Set Object
(
[_elts:Shippo_Util_Set:private] => Array
(
)
)
[_transientValues:protected] => Shippo_Util_Set Object
(
[_elts:Shippo_Util_Set:private] => Array
(
)
)
[_retrieveOptions:protected] => Array
(
)
)
[metadata] =>
[tracking_status] => Shippo_Object Object
(
[_apiKey:protected] => XXXXXXXXXXXXX
[_values:protected] => Array
(
[object_created] => 2017-08-04T23:05:43.651Z
[object_updated] => 2017-08-04T23:05:43.651Z
[object_id] => XXXXXXXXXXXXX
[status] => UNKNOWN (¡¡¡This is what I want to select!!!!)
[status_details] => Shipment information received
[status_date] => 2017-08-04T06:40:00Z
[location] =>
)
[_unsavedValues:protected] => Shippo_Util_Set Object
(
[_elts:Shippo_Util_Set:private] => Array
(
)
)
[_transientValues:protected] => Shippo_Util_Set Object
(
[_elts:Shippo_Util_Set:private] => Array
(
)
)
[_retrieveOptions:protected] => Array
(
)
)
[tracking_history] => Array
(
[0] => Shippo_Object Object
(
[_apiKey:protected] => XXXXXXXXXXXXX
[_values:protected] => Array
(
[object_created] => 2017-08-04T23:05:43.651Z
[object_id] => XXXXXXXXXXXXX
[status] => UNKNOWN
[status_details] => Shipment information received
[status_date] => 2017-08-04T06:40:00Z
[location] =>
)
[_unsavedValues:protected] => Shippo_Util_Set Object
(
[_elts:Shippo_Util_Set:private] => Array
(
)
)
[_transientValues:protected] => Shippo_Util_Set Object
(
[_elts:Shippo_Util_Set:private] => Array
(
)
)
[_retrieveOptions:protected] => Array
(
)
)
)
[transaction] =>
[messages] => Array
(
)
)
[*_unsavedValues] => Shippo_Util_Set Object
(
[_elts:Shippo_Util_Set:private] => Array
(
)
)
[*_transientValues] => Shippo_Util_Set Object
(
[_elts:Shippo_Util_Set:private] => Array
(
)
)
[*_retrieveOptions] => Array
(
)
)
这是同一个数组的var_dump:
array(5) {
["*_apiKey"]=>
string(52) "XXXXXXXXXXXXXXXX"
["*_values"]=>
array(11) {
["carrier"]=>
string(11) "dhl_express"
["tracking_number"]=>
string(10) "XXXXXXXXX"
["address_from"]=>
array(5) {
["*_apiKey"]=>
string(52) "XXXXXXXXXXXXXXXX"
["*_values"]=>
array(4) {
["city"]=>
string(9) "xxxxxxxxxxx"
["state"]=>
string(0) ""
["zip"]=>
string(5) "xxxxx"
["country"]=>
string(2) "MX"
}
["*_unsavedValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_transientValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_retrieveOptions"]=>
array(0) {
}
}
["address_to"]=>
array(5) {
["*_apiKey"]=>
string(52) "XXXXXXXXXXXXXXXX"
["*_values"]=>
array(4) {
["city"]=>
string(9) "xxxxxxxxx"
["state"]=>
string(0) ""
["zip"]=>
string(5) "xxxxxxxxx"
["country"]=>
string(2) "MX"
}
["*_unsavedValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_transientValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_retrieveOptions"]=>
array(0) {
}
}
["eta"]=>
NULL
["servicelevel"]=>
array(5) {
["*_apiKey"]=>
string(52) "XXXXXXXXXXXXXXXX"
["*_values"]=>
array(2) {
["token"]=>
NULL
["name"]=>
NULL
}
["*_unsavedValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_transientValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_retrieveOptions"]=>
array(0) {
}
}
["metadata"]=>
NULL
["tracking_status"]=>
array(5) {
["*_apiKey"]=>
string(52) "XXXXXXXXXXXXXXXX"
["*_values"]=>
array(7) {
["object_created"]=>
string(24) "2017-08-07T17:10:41.233Z"
["object_updated"]=>
string(24) "2017-08-07T17:10:41.233Z"
["object_id"]=>
string(32) "xxxxxxxxxxxxxxxxxxxxxxx"
["status"]=>
string(7) "TRANSIT"
["status_details"]=>
string(21) "With delivery courier"
["status_date"]=>
string(20) "2017-08-07T10:04:14Z"
["location"]=>
NULL
}
["*_unsavedValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_transientValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_retrieveOptions"]=>
array(0) {
}
}
["tracking_history"]=>
array(6) {
[0]=>
array(5) {
["*_apiKey"]=>
string(52) "XXXXXXXXXXXXXXXX"
["*_values"]=>
array(6) {
["object_created"]=>
string(24) "2017-08-04T23:05:43.651Z"
["object_id"]=>
string(32) "xxxxxxxxxxxxxxxxxxxxx"
["status"]=>
string(7) "UNKNOWN"
["status_details"]=>
string(29) "Shipment information received"
["status_date"]=>
string(20) "2017-08-04T06:40:00Z"
["location"]=>
NULL
}
["*_unsavedValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_transientValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_retrieveOptions"]=>
array(0) {
}
}
[1]=>
array(5) {
["*_apiKey"]=>
string(52) "XXXXXXXXXXXXXXXX"
["*_values"]=>
array(6) {
["object_created"]=>
string(24) "2017-08-07T17:10:41.233Z"
["object_id"]=>
string(32) "xxxxxxxxxxxxxxxxxxxx"
["status"]=>
string(7) "TRANSIT"
["status_details"]=>
string(18) "Shipment picked up"
["status_date"]=>
string(20) "2017-08-04T20:20:56Z"
["location"]=>
NULL
}
["*_unsavedValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_transientValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_retrieveOptions"]=>
array(0) {
}
}
[2]=>
array(5) {
["*_apiKey"]=>
string(52) "XXXXXXXXXXXXXXXX"
["*_values"]=>
array(6) {
["object_created"]=>
string(24) "2017-08-07T17:10:41.233Z"
["object_id"]=>
string(32) "xxxxxxxxxxxxxxxxxxxxxx"
["status"]=>
string(7) "TRANSIT"
["status_details"]=>
string(31) "Processed at MONTERREY - MEXICO"
["status_date"]=>
string(20) "2017-08-04T21:09:30Z"
["location"]=>
NULL
}
["*_unsavedValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_transientValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_retrieveOptions"]=>
array(0) {
}
}
[3]=>
array(5) {
["*_apiKey"]=>
string(52) "XXXXXXXXXXXXXXXX"
["*_values"]=>
array(6) {
["object_created"]=>
string(24) "2017-08-07T17:10:41.233Z"
["object_id"]=>
string(32) "xxxxxxxxxxxxxxxxxxxxxx"
["status"]=>
string(7) "TRANSIT"
["status_details"]=>
string(39) "Departed Facility in MONTERREY - MEXICO"
["status_date"]=>
string(20) "2017-08-05T00:16:59Z"
["location"]=>
NULL
}
["*_unsavedValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_transientValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_retrieveOptions"]=>
array(0) {
}
}
[4]=>
array(5) {
["*_apiKey"]=>
string(52) "XXXXXXXXXXXXXXXX"
["*_values"]=>
array(6) {
["object_created"]=>
string(24) "2017-08-07T17:10:41.233Z"
["object_id"]=>
string(32) "710e995c15654532bb23425b4c30426e"
["status"]=>
string(7) "TRANSIT"
["status_details"]=>
string(74) "Arrived at Delivery Facility in
CHIHUAHUA - MEXICO"
["status_date"]=>
string(20) "2017-08-07T08:02:41Z"
["location"]=>
NULL
}
["*_unsavedValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_transientValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_retrieveOptions"]=>
array(0) {
}
}
[5]=>
array(5) {
["*_apiKey"]=>
string(52) "XXXXXXXXXXXXXXXX"
["*_values"]=>
array(6) {
["object_created"]=>
string(24) "2017-08-07T17:10:41.233Z"
["object_id"]=>
string(32) "XXXXXXXXXXXX"
["status"]=>
string(7) "TRANSIT"
["status_details"]=>
string(21) "With delivery courier"
["status_date"]=>
string(20) "2017-08-07T10:04:14Z"
["location"]=>
NULL
}
["*_unsavedValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_transientValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_retrieveOptions"]=>
array(0) {
}
}
}
["transaction"]=>
NULL
["messages"]=>
array(0) {
}
}
["*_unsavedValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_transientValues"]=>
array(1) {
["Shippo_Util_Set_elts"]=>
array(0) {
}
}
["*_retrieveOptions"]=>
array(0) {
}
}
以下是我试图找到它的方法($tracking_dhl
是上面的数组):
$tracking_dhl_status = (array) $tracking_dhl["*_values"]["tracking_status"];
echo "Traking status: " . $tracking_dhl_status["_values:protected"]["status"] . "<br>";
但是我总是得到一个错误,即密钥不存在。
答案 0 :(得分:0)
所以我提出了一个解决方法,正如在另一个线程中所建议的那样,我使用foreach循环将对象更改为数组,其中我使用了键并且错误地使用了* _和presto。这是代码:
public function toArray($obj) {
if (is_object($obj)) $obj = (array)$obj;
if (is_array($obj)) {
$new = array();
foreach ($obj as $key => $val) {
if (strpos(substr($key,0, 4), '_') !== false) {
$new[substr($key, 4)] = $this->toArray($val);
} else {
$new[$key] = $this->toArray($val);
}
//$new[str_replace("_", "", $key)] = $this->toArray($val);
}
} else {
$new = $obj;
}
return $new;
}
这个函数帮助我将对象转换为数组并更改键名以取出* _,实际上使用4个字符。我还是不明白。即便如此,你可以摆脱对象中令人讨厌的受保护物品哈哈。我看到另一个人ltrim()
而不是substr()
,如果我做了,但我不知道为什么ltrim()
对我不起作用。我希望这可以帮助任何人。