私の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) {
}
}
そして、ここでは、Imは($tracking_dhl
は、上記の配列である)それを取得しようとしている方法です。
$tracking_dhl_status = (array) $tracking_dhl["*_values"]["tracking_status"];
echo "Traking status: " . $tracking_dhl_status["_values:protected"]["status"] . "<br>";
しかし、私はいつもキーが存在しないというエラーを受け取ります。
保護されたクラスメンバーにアクセスしようとしていますが、これは配列ではありません。 ':protected'という名前は、クラスを配列として出力しようとしているprint_rにすぎず、メンバの保護タイプを通知します。 'print_r'の代わりに' var_dump'を使って、あなたがダンプしている変数に関する詳細情報を取得してください。 – Geoffrey
まず、これらは配列ではなくオブジェクトです。したがって、あなたの "API"(その部分は詳しく述べる価値があります)は既にデータ構造を内部オブジェクト表現に変換します。アクセッサラッパー(多かれ少なかれ感覚的なもの)があることを意味します。 – mario
このオブジェクトは、私がAPIに行う呼び出しで返されるものです。次にオブジェクトを配列にキャストし、shippmentのステータスを取得しようとします。それが* _の場合、私はその価値を得るために何をしなければならないでしょうか? –