0

I send a request and get back JSON, so I convert that JSON to an array, but I cannot use the array with my php:

$all_transactions_raw = file_get_contents("xxx");

$all_transactions = json_decode( $all_transactions_raw );

 foreach ( $all_transaction as $transaction ) {
    echo $transaction;
  }

First part of the very long output:

Array ( 
    [data] => Array ( 
        [transactions] => Array ( 
            [0] => Array ( 
                [id] => t1rtn-gsjkadfv 
                [date] => 2017-11-01 
                [amount] => -8847
                [memo] => 
                [cleared] => reconciled 
                [approved] => 1 
                [flag_color] => 

why does the above code not print a list of transactions given by the api request?

when I use print_r( $all_transactions ); I can print out a long string of what looks like an array but I cannot access the data contained in it as separate chunks. Am I missing something?

8
  • show us your expected json result that you want to work with. Commented Dec 30, 2019 at 16:14
  • $transaction is also probably an array, so try print_r($transaction); in the loop as echo will probably fail. Commented Dec 30, 2019 at 16:14
  • You should also add true as the second argument to json_decode() to assure that you get the result as associative arrays instead of object. However, since we have no idea what the result looks like, I can't know if that's the issue or not. Please share the value of $all_transactions_raw. Commented Dec 30, 2019 at 16:18
  • @ttrasn I want to work with the result as an array so that I can say $all_transactions[1] and do something with that. Commented Dec 30, 2019 at 16:25
  • 1
    Something's strange here. If you're using json_decode() without true as a second argument like in your code, I don't see how the result could be associative arrays instead of objects? What exactly is that array a dump of? The raw value or inside the loop? Commented Dec 30, 2019 at 16:29

1 Answer 1

1

i guess you are missing these keys in foreach

$all_transactions_raw = file_get_contents("xxx");

$all_transactions = json_decode( $all_transactions_raw );

 foreach ( $all_transaction['data']['transactions'] as $transaction ) {
    echo $transaction;
  }
Sign up to request clarification or add additional context in comments.

6 Comments

yes! that did it, I can work with single transactions now using that, thanks
I'm sorry, but I'm failing to see how this would work at all? How can $all_transactions possibly be an associative array with the above code?
@MagnusEriksson idk but I am able to do ` print_r( $all_transactions['data']['transactions'][0] ); ` which prints out an array of all data associated with the 0th transaction in the output
if you have online one array then you can do like $all_transactions['data']['transactions'][0] otherwise you should need loop
@fall3n - Yes, but as you said in your comment, you are actually using true as the second argument. This answer doesn't so it's actually wrong and could cause confusion for future visitors seeing it and trying it.
|

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.