0

I need to see all addresses, amounts, and confirmations foreach transaction - except each array is different. How can I get all this information from the litecoind's response?

                foreach($transactions as $transaction) {

                    echo '<br />' . $transactions[0]['address'];
                    echo '<br />' . $transactions[0]['amount'];
                    echo '<br />' . $transactions[0]['confirmations'];

                }

Above is the PHP Code, the response comes from these lines:

$transactions = $litecoin->listreceivedbyaddress();


var_dump($transactions);


array(2) { [0]=> array(5) { ["address"]=> string(34) "LPwNe6JtUgXxWrdK26UMKJJrDwYzEzkUZY" ["account"]=> string(0) "" ["amount"]=> float(1) ["confirmations"]=> int(4) ["txids"]=> array(1) { [0]=> string(64) "9da67825c00cf01c991e2fa913cc82c59d558b4c41d8f4e3dcb8f862b81affec" } } [1]=> array(5) { ["address"]=> string(34) "LTfP4riFBn6ctQ9jbGyWZ2HswmUZKgrJbX" ["account"]=> string(0) "" ["amount"]=> float(1) ["confirmations"]=> int(537) ["txids"]=> array(1) { [0]=> string(64) "b9806a0adc91ef70e67341f7d0cf6f2c7422fac35e54062153318d4416f44eaf" } } } 

This is what it returns:

LPwNe6JtUgXxWrdK26UMKJJrDwYzEzkUZY
1
4
LPwNe6JtUgXxWrdK26UMKJJrDwYzEzkUZY
1
4

As you can see it returns the same thing, how can I have it return the two different transactions? Thanks.

John Smith
  • 75
  • 2
  • 7

2 Answers2

3

You are accessing the array's first element every time. Use the variable $transaction which will take on each value of the array in turn.

change:

echo '<br />' . $transactions[0]['address'];

to

echo '<br />' . $transaction['address'];

and similarly for the other lines.

Amal Murali
  • 73,160
  • 18
  • 123
  • 143
Lyn Headley
  • 10,848
  • 3
  • 32
  • 35
-1

how can I have it return the two different transactions?

By iterating over both transactions, let's say you have them in an array called $array, then it works like:

foreach ($array as $value) {
    var_dump($value);
}

That simple it is, really. Compare http://php.net/foreach.

Now compare with your code, you're not that far away:

On the one hand you obviously foreach that $transactions array:

foreach ($transactions as $transaction) 
{
    ...
}

Because you get two-times some output.

But on the other hand you doesn't foreach that $transactions array:

    echo '<br />' . $transactions[0]['address'];    
    echo '<br />' . $transactions[0]['amount'];
    echo '<br />' . $transactions[0]['confirmations'];

So you probably should decide what you want. Foreaching the array or just accessing it by it's numeric indecies. PHP will do as you write the code for it.

As you've got experienced both works. To see your code in foreach action:

foreach ($transactions as $transaction) {
    var_dump($transaction);
}

And to see it in non-foreach action:

var_dump($transactions[0]);
var_dump($transactions[1]);

which is just accessing the array, compare http://php.net/arrays

hakre
  • 184,866
  • 48
  • 414
  • 792