如何在PHP中获取此数据类型的关键“错误”

时间:2017-11-11 05:26:33

标签: php arrays json

我的数据类型如下所示。但我想得到'错误'键和'errors.message'值。

[  
   {  
      "payout_item_id":"HIDDEN",
      "transaction_id":"HIDDEN",
      "transaction_status":"UNCLAIMED",
      "payout_item_fee":{  
         "currency":"USD",
         "value":"10.25"
      },
      "payout_batch_id":"HIDDEN",
      "payout_item":{  
         "amount":{  
            "currency":"USD",
            "value":"10.00"
         },
         "note":"Thanks for your patronage",
         "receiver":"HIDDEN@gmail.com",
         "recipient_type":"EMAIL",
         "sender_item_id":"1510369638"
      },
      "time_processed":"2017-11-11T03:07:26Z",
      "errors":{  
         "name":"RECEIVER_UNCONFIRMED",
         "message":"Receiver is unconfirmed",
         "information_link":"https:\/\/developer.paypal.com\/docs\/api\/payments.payouts-batch\/#errors"
      },
      "links":[  
         {  
            "href":"https:\/\/api.sandbox.paypal.com\/v1\/payments\/payouts-item\/HIDDEN",
            "rel":"item",
            "method":"GET"
         }
      ]
   }
]

我试图将代码存储在变量($ get_errors)中,然后像这样调用'errors'键。但代码不起作用。

return $get_errors['errors']; 
return $get_errors->errors;

感谢。

3 个答案:

答案 0 :(得分:0)

尝试将您的JSON数据转换为数组然后从数组中获取所需的结果尝试以下可能它适合您

$getError = '[  
   {  
      "payout_item_id":"HIDDEN",
      "transaction_id":"HIDDEN",
      "transaction_status":"UNCLAIMED",
      "payout_item_fee":{  
         "currency":"USD",
         "value":"10.25"
      },
      "payout_batch_id":"HIDDEN",
      "payout_item":{  
         "amount":{  
            "currency":"USD",
            "value":"10.00"
         },
         "note":"Thanks for your patronage",
         "receiver":"HIDDEN@gmail.com",
         "recipient_type":"EMAIL",
         "sender_item_id":"1510369638"
      },
      "time_processed":"2017-11-11T03:07:26Z",
      "errors":{  
         "name":"RECEIVER_UNCONFIRMED",
         "message":"Receiver is unconfirmed",
         "information_link":"https:\/\/developer.paypal.com\/docs\/api\/payments.payouts-batch\/#errors"
      },
      "links":[  
         {  
            "href":"https:\/\/api.sandbox.paypal.com\/v1\/payments\/payouts-item\/HIDDEN",
            "rel":"item",
            "method":"GET"
         }
      ]
   }
]';
$array_getError  = json_decode($getError,true);
var_dump($array_getError[0]["errors"]);

答案 1 :(得分:0)

这是Json类型。所以你必须解码json。然后你可以从中获取数据

$json = '[  
   {  
      "payout_item_id":"HIDDEN",
      "transaction_id":"HIDDEN",
      "transaction_status":"UNCLAIMED",
      "payout_item_fee":{  
         "currency":"USD",
         "value":"10.25"
      },
      "payout_batch_id":"HIDDEN",
      "payout_item":{  
         "amount":{  
            "currency":"USD",
            "value":"10.00"
         },
         "note":"Thanks for your patronage",
         "receiver":"HIDDEN@gmail.com",
         "recipient_type":"EMAIL",
         "sender_item_id":"1510369638"
      },
      "time_processed":"2017-11-11T03:07:26Z",
      "errors":{  
         "name":"RECEIVER_UNCONFIRMED",
         "message":"Receiver is unconfirmed",
         "information_link":"https:\/\/developer.paypal.com\/docs\/api\/payments.payouts-batch\/#errors"
      },
      "links":[  
         {  
            "href":"https:\/\/api.sandbox.paypal.com\/v1\/payments\/payouts-item\/HIDDEN",
            "rel":"item",
            "method":"GET"
         }
      ]
   }
]';

$data = json_decode($json,true);
echo $data[0]['errors']['message'];

DEMO

答案 2 :(得分:0)

$x = '[  
   {  
      "payout_item_id":"HIDDEN",
      "transaction_id":"HIDDEN",
      "transaction_status":"UNCLAIMED",
      "payout_item_fee":{  
         "currency":"USD",
         "value":"10.25"
      },
      "payout_batch_id":"HIDDEN",
      "payout_item":{  
         "amount":{  
            "currency":"USD",
            "value":"10.00"
         },
         "note":"Thanks for your patronage",
         "receiver":"HIDDEN@gmail.com",
         "recipient_type":"EMAIL",
         "sender_item_id":"1510369638"
      },
      "time_processed":"2017-11-11T03:07:26Z",
      "errors":{  
         "name":"RECEIVER_UNCONFIRMED",
         "message":"Receiver is unconfirmed",
         "information_link":"https:\/\/developer.paypal.com\/docs\/api\/payments.payouts-batch\/#errors"
      },
      "links":[  
         {  
            "href":"https:\/\/api.sandbox.paypal.com\/v1\/payments\/payouts-item\/HIDDEN",
            "rel":"item",
            "method":"GET"
         }
      ]
   }
]';

$errors = (json_decode($x)[0]->errors);