我已将返回的API对象响应放入数组中,但现在我想访问数组数据,我得到未定义的索引,这是我的代码。
$paystack = new Yabacon\Paystack('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$trx = $paystack->transactions(["perPage"=>1, "amount"=>10000, "status"=>"success"]); //RETURNED OBJECT
$array = (array) $trx; //CONVERTED FROM OBJECT TO ARRAY.
当我var_dump( (array) $trx );
得到
array(4) { ["status"]=> bool(true) ["message"]=> string(22) "Transactions retrieved" ["data"]=> array(1) { [0]=> object(stdClass)#21 (22) { ["id"]=> int(901742) ["domain"]=> string(4) "test" ["status"]=> string(7) "success" ["reference"]=> string(13) "58c0e61ca5cce" ["amount"]=> int(10000) ["message"]=> NULL ["gateway_response"]=> string(10) "Successful" ["paid_at"]=> string(24) "2017-03-08T22:48:33.000Z" ["created_at"]=> string(24) "2017-03-08T22:48:11.000Z" ["channel"]=> string(4) "card" ["currency"]=> string(3) "NGN" ["ip_address"]=> string(14) "xxx.xxx.xxx.xxx" ["metadata"]=> object(stdClass)#23 (2) { ["custom_fields"]=> array(1) { [0]=> object(stdClass)#22 (3) { ["display_name"]=> string(13) "Mobile Number" ["variable_name"]=> string(13) "mobile_number" ["value"]=> string(14) "+xxxxxxxxxxxxx" } } ["referrer"]=> string(27) "http://localhost/b/checkout" } ["log"]=> object(stdClass)#24 (9) { ["time_spent"]=> int(24) ["attempts"]=> int(1) ["authentication"]=> NULL ["errors"]=> int(0) ["success"]=> bool(true) ["mobile"]=> bool(false) ["input"]=> array(0) { } ["channel"]=> NULL ["history"]=> array(4) { [0]=> object(stdClass)#25 (3) { ["type"]=> string(5) "input" ["message"]=> string(55) "Filled these fields: card number, card expiry, card cvv" ["time"]=> int(20) } [1]=> object(stdClass)#26 (3) { ["type"]=> string(6) "action" ["message"]=> string(16) "Attempted to pay" ["time"]=> int(20) } [2]=> object(stdClass)#27 (3) { ["type"]=> string(7) "success" ["message"]=> string(17) "Successfully paid" ["time"]=> int(22) } [3]=> object(stdClass)#28 (3) { ["type"]=> string(5) "close" ["message"]=> string(11) "Page closed" ["time"]=> int(24) } } } ["fees"]=> int(150) ["fees_split"]=> NULL ["customer"]=> object(stdClass)#29 (8) { ["id"]=> int(102185) ["first_name"]=> string(0) "" ["last_name"]=> string(0) "" ["email"]=> string(23) "xxxxxxxxxx@gmail.com" ["customer_code"]=> string(19) "CUS_xxxxxxxxxxx" ["phone"]=> string(0) "" ["metadata"]=> NULL ["risk_action"]=> string(7) "default" } ["authorization"]=> object(stdClass)#30 (12) { ["authorization_code"]=> string(15) "AUTH_xxxxxxxxx" ["bin"]=> string(6) "412345" ["last4"]=> string(4) "1381" ["exp_month"]=> string(2) "01" ["exp_year"]=> string(4) "2020" ["channel"]=> string(4) "card" ["card_type"]=> string(4) "visa" ["bank"]=> string(9) "TEST BANK" ["country_code"]=> string(2) "NG" ["brand"]=> string(4) "visa" ["reusable"]=> bool(true) ["signature"]=> string(24) "SIG_xxxxxxxxxxxx" } ["plan"]=> object(stdClass)#31 (0) { } ["subaccount"]=> object(stdClass)#32 (0) { } ["paidAt"]=> string(24) "2017-03-08T22:48:33.000Z" ["createdAt"]=> string(24) "2017-03-08T22:48:11.000Z" } } ["meta"]=> object(stdClass)#33 (6) { ["total"]=> int(6) ["total_volume"]=> int(60000) ["skipped"]=> int(0) ["perPage"]=> string(1) "1" ["page"]=> int(1) ["pageCount"]=> int(6) } }
我想要做的是,我希望以这种格式获得它
$stats = $array['data']['status'];
$order_id = $array['data']['id'];
$transaction_method = $array['data']['channel'];
$currency = $array['data']['currency'];
$reference = $array['data']['reference'];
$final_price = $array['data']['amount'];
$email_send = $array['data']['customer']['email'];
$day_paid = $array['data']['paid_at'];
$referring_page = $array['data']['metadata']['referrer'];
但当我ECHO
任何变量时,例如我echo $stats
或echo $order_id
我得到
Notice: Undefined index: status in C:\xampp\htdocs\b\d\paystackorders.php on line 150
Notice: Undefined index: id in C:\xampp\htdocs\b\d\paystackorders.php on line 151.
我如何解决此问题。
答案 0 :(得分:1)
需要$array['data'][0]['status']
和$array['data'][0]['id']
等等...
这是因为$ array ['data']是一个对象数组
我认为最好的方法如下:
$data = $array['data'][0];
然后访问您想要的内容:
$status = $data['status'];
$order_id = $data['id'];