0

i have put a returned API object response into an array but now i want to access the array data, i get undefined index, this is my code.

 $paystack = new Yabacon\Paystack('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

 $trx = $paystack->transactions(["perPage"=>1, "amount"=>10000, "status"=>"success"]); //RETURNED OBJECT

 $array =  (array) $trx; //CONVERTED FROM OBJECT TO ARRAY.

When I var_dump( (array) $trx ); i get

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) "[email protected]" ["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) } }

What I want to do is, I want to get it in this format

$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']; 

But when i ECHO any variable out e.g i do echo $stats OR echo $order_id i get

 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.

How do i go around resolving this.

1
  • looks like data is an array ... ['data'][0] ? if that solves this issue, ponder as to why you are getting an array, and what you should do in case more are coming. Commented Oct 10, 2017 at 16:45

1 Answer 1

1

It needs to be $array['data'][0]['status'] and $array['data'][0]['id'] and so on... This is because $array['data'] is an array of objects

The best way to go in my opinion is as follows:

$data = $array['data'][0];

And then access what you want :

$status = $data['status'];
$order_id = $data['id'];
Sign up to request clarification or add additional context in comments.

2 Comments

this is what i get when i run that again "Fatal error: Uncaught Error: Cannot use object of type stdClass as array in C:\xampp\htdocs\b\d\paystackorders.php:154 Stack trace: #0 {main} thrown in C:\xampp\htdocs\b\d\paystackorders.php on line 154" @mrateb
$array['data'] is an array of objects, its not an object. $array['data'][0] is an object however. Its better if show us what you're running now so we can help with this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.