无法使用其密钥名上带有* _的密钥来选择数组值
问题描述:
使用一个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>";
但我总是得到一个错误,该关键不存在。
答
所以我想出了一个解决方法,正如另一个线程中所建议的那样,我使用foreach循环将对象更改为数组,并在其中使用了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()
不适合我。我希望这可以帮助任何人。
您正试图访问受保护的类成员,这些不是数组。名称':protected'只是print_r试图以数组的形式打印一个类,通知你成员的保护类型。使用'var_dump'而不是'print_r'来获得更多关于你正在抛出的变量的信息。 – Geoffrey
首先,这些不是数组,而是对象。因此,您的“API”(→该部分值得详细阐述)已经将数据结构转换为内部对象表示。这通常意味着有访问者包装器(或多或少有意义的包装器)。 – mario
那么这个对象就是我在API调用中返回给我的东西。然后,我将这个对象放到数组中,并尝试获得分发的状态。如果它具有那个* _,那么我需要做些什么才能获得该值? –