user113470: Currently im trying to build an explorer and im stuck on the block/listing transactions.
Currently this is my code.
foreach($info['tx'] AS $key => $value){
            $txinfo = callBTC("getrawtransaction",array("\"$value\"",true));
            echo '<pre>';
            print_r($txinfo);
            echo '</pre>';
            echo '<div class="tx"><div class="txRow"><div class="txCat">Hash</div><div class="txInfo"><div class="overflow"><a href="index?txid='.$value.'">'.$value.'</a></div></div></div>';
            echo '<div class="txRow"><div class="txCat">Time</div><div class="txInfo">'.date("Y-m-d H:i:s",$txinfo['time']).'</div></div>';
            echo '<div class="txRow"><div class="txCat">From</div><div class="txInfo"><div class="overflow">';
            foreach($txinfo['vin'] AS $key2 => $value2){
                if(!empty($txinfo['vin'][$key2]['coinbase'])){
                    echo '<div class="confirmed">COINBASE - Newly Created Coins</div>';
                }else{
                    $from = callBTC("getrawtransaction",array("\"$value2[txid]\"",true));
                    echo '<pre>';
                    print_r($from);
                    echo '</pre>';
                    echo $from['vout'][0]['scriptPubKey']['addresses'][0].'<br/>';
                }
            }
            echo '</div></div></div>';
            echo '<div class="txRow"><div class="txCat">To</div><div class="txInfo"><div class="overflow">';
            foreach($txinfo['vout'] AS $key3 => $value3){
                if(!empty($value3['scriptPubKey']['addresses'])){
                    foreach($value3['scriptPubKey']['addresses'] AS $key4 => $value4){
                        echo '<a href="index?addid='.$value4.'">'.$value4.'</a><br/>';
                    }
                }
            }
            echo '</div></div></div>';
            echo '<div class="txRow"><div class="txCat">Fee</div><div class="txInfo"></div></div>';
            echo '<div class="txRow"><div class="txCat">Amount</div><div class="txInfo"></div></div></div>';
        }

i got the to addresses fine but whne it comes to the from field i struggle to determine which is the valid address to use.
Lets use block 2092235 for example
https://www.blockchain.com/btc-testnet/block/000000000000003f69d7c8cb4943df2d7a273883eb72024f3305122ed2dc7271?page=1
I use blockchain.com to check against my site to ensure im displaying the proper data.
but when i spit out the transaction for the tx id ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6 i get this...
Array
(
    [txid] => ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6
    [hash] => b388c750f3d15f1fcdb320fbc3398a65a34a3eb6933ca1738729f22a4a8a5924
    [version] => 2
    [size] => 222
    [vsize] => 141
    [weight] => 561
    [locktime] => 2092234
    [vin] => Array
        (
            [0] => Array
                (
                    [txid] => 122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c
                    [vout] => 1
                    [scriptSig] => Array
                        (
                            [asm] => 
                            [hex] => 
                        )

                    [txinwitness] => Array
                        (
                            [0] => 304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401
                            [1] => 0258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280
                        )

                    [sequence] => 4294967294
                )

        )

    [vout] => Array
        (
            [0] => Array
                (
                    [value] => 0.01905871
                    [n] => 0
                    [scriptPubKey] => Array
                        (
                            [asm] => 0 2cda63f440de6ab858cf2550daf3b25522d6c66f
                            [hex] => 00142cda63f440de6ab858cf2550daf3b25522d6c66f
                            [reqSigs] => 1
                            [type] => witness_v0_keyhash
                            [addresses] => Array
                                (
                                    [0] => tb1q9ndx8azqme4tskx0y4gd4uaj253dd3n0386xkn
                                )

                        )

                )

            [1] => Array
                (
                    [value] => 72.44723495
                    [n] => 1
                    [scriptPubKey] => Array
                        (
                            [asm] => 0 e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab
                            [hex] => 0014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab
                            [reqSigs] => 1
                            [type] => witness_v0_keyhash
                            [addresses] => Array
                                (
                                    [0] => tb1qu9u6609s6w798ja9vnxzupdu9hqcmcdtvm87gx
                                )

                        )

                )

        )

    [hex] => 020000000001015ced69c0d591aa8616d5be813dca34382be7ff86e8215d6c1dfb36226b912e120100000000feffffff02cf141d00000000001600142cda63f440de6ab858cf2550daf3b25522d6c66f27b5d1af01000000160014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab0247304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401210258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280caec1f00
    [blockhash] => 000000000000003f69d7c8cb4943df2d7a273883eb72024f3305122ed2dc7271
    [confirmations] => 1418
    [time] => 1631039838
    [blocktime] => 1631039838
)

then i get the tx id from the vin field to get info on the transaction for that address(the from field) and i get this.
Array
(
    [txid] => 122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c
    [hash] => 5fb52307d771c7dadf9f06006340a0acb5ff9ebe15d215d98f08e749d0ee2e59
    [version] => 2
    [size] => 222
    [vsize] => 141
    [weight] => 561
    [locktime] => 2092232
    [vin] => Array
        (
            [0] => Array
                (
                    [txid] => 2739adc2b59de66a9e707ec3e102549dc56a8432e38ef471cb961976a27739be
                    [vout] => 0
                    [scriptSig] => Array
                        (
                            [asm] => 
                            [hex] => 
                        )

                    [txinwitness] => Array
                        (
                            [0] => 3044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e01
                            [1] => 036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296a
                        )

                    [sequence] => 4294967294
                )

        )

    [vout] => Array
        (
            [0] => Array
                (
                    [value] => 0.01275647
                    [n] => 0
                    [scriptPubKey] => Array
                        (
                            [asm] => 0 184337352372a5fa6218dd57ea1057305d39c77d
                            [hex] => 0014184337352372a5fa6218dd57ea1057305d39c77d
                            [reqSigs] => 1
                            [type] => witness_v0_keyhash
                            [addresses] => Array
                                (
                                    [0] => tb1qrppnwdfrw2jl5cscm4t75yzhxpwnn3mamh6ff8
                                )

                        )

                )

            [1] => Array
                (
                    [value] => 72.46645714
                    [n] => 1
                    [scriptPubKey] => Array
                        (
                            [asm] => 0 c4520316f8d1dee0a19acde950d0540701d3d013
                            [hex] => 0014c4520316f8d1dee0a19acde950d0540701d3d013
                            [reqSigs] => 1
                            [type] => witness_v0_keyhash
                            [addresses] => Array
                                (
                                    [0] => tb1qc3fqx9hc680wpgv6eh54p5z5quqa85qn35meep
                                )

                        )

                )

        )

    [hex] => 02000000000101be3977a2761996cb71f48ee332846ac59d5402e1c37e709e6ae69db5c2ad39270000000000feffffff02ff76130000000000160014184337352372a5fa6218dd57ea1057305d39c77dd209efaf01000000160014c4520316f8d1dee0a19acde950d0540701d3d01302473044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e0121036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296ac8ec1f00
    [blockhash] => 00000000000000d299298de1cfb14276667b7d55e8a1cf43c9dc825babadf058
    [confirmations] => 1420
    [time] => 1631039256
    [blocktime] => 1631039256
)

by looking at blockchain i know which address it SHOULD take (tb1qc3fqx9hc680wpgv6eh54p5z5quqa85qn35meep)...but when i use php and check for last key in array its not always the correct address in other transactions as sometimes the address i need is in a different index(same array tho just different index).
How can i programmatically determine the right address consistently?

user-1: 
i got the to addresses fine but when it comes to the from field i struggle to determine which is the valid address to use.

There is no 'from' address in any transaction as mentioned by Sipa in the comments.
Is it possible to assign a single "from" and a single "to" address to Bitcoin transactions?

but when i spit out the transaction for the tx id ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6


How can i programmatically determine the right address consistently?

I will share steps to check inputs and outputs for this transaction using Bitcoin Core:
$ bitcoin-cli getrawtransaction 0d45dacc0e50066a55c13b54cdcd6e365bb90be3a492c55bf4200b01d02fd748


020000000001015ced69c0d591aa8616d5be813dca34382be7ff86e8215d6c1dfb36226b912e120100000000feffffff02cf141d00000000001600142cda63f440de6ab858cf2550daf3b25522d6c66f27b5d1af01000000160014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab0247304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401210258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280caec1f00

$ bitcoin-cli decoderawtransaction 020000000001015ced69c0d591aa8616d5be813dca34382be7ff86e8215d6c1dfb36226b912e120100000000feffffff02cf141d00000000001600142cda63f440de6ab858cf2550daf3b25522d6c66f27b5d1af01000000160014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab0247304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401210258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280caec1f00

{
  "txid": "ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6",
  "hash": "b388c750f3d15f1fcdb320fbc3398a65a34a3eb6933ca1738729f22a4a8a5924",
  "version": 2,
  "size": 222,
  "vsize": 141,
  "weight": 561,
  "locktime": 2092234,
  "vin": [
    {
      "txid": "122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c",
      "vout": 1,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "txinwitness": [
        "304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401",
        "0258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280"
      ],
      "sequence": 4294967294
    }
  ],
  "vout": [
    {
      "value": 0.01905871,
      "n": 0,
      "scriptPubKey": {
        "asm": "0 2cda63f440de6ab858cf2550daf3b25522d6c66f",
        "hex": "00142cda63f440de6ab858cf2550daf3b25522d6c66f",
        "address": "tb1q9ndx8azqme4tskx0y4gd4uaj253dd3n0386xkn",
        "type": "witness_v0_keyhash"
      }
    },
    {
      "value": 72.44723495,
      "n": 1,
      "scriptPubKey": {
        "asm": "0 e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab",
        "hex": "0014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab",
        "address": "tb1qu9u6609s6w798ja9vnxzupdu9hqcmcdtvm87gx",
        "type": "witness_v0_keyhash"
      }
    }
  ]
}





Inputs
Outputs




"txid": "122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c", "vout": 1
tb1q9ndx8azqme4tskx0y4gd4uaj253dd3n0386xkn (0.01905871)



tb1qu9u6609s6w798ja9vnxzupdu9hqcmcdtvm87gx (72.44723495)




To get more information about input being spent in this transaction:
$ bitcoin-cli getrawtransaction 122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c


02000000000101be3977a2761996cb71f48ee332846ac59d5402e1c37e709e6ae69db5c2ad39270000000000feffffff02ff76130000000000160014184337352372a5fa6218dd57ea1057305d39c77dd209efaf01000000160014c4520316f8d1dee0a19acde950d0540701d3d01302473044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e0121036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296ac8ec1f00

$ bitcoin-cli decoderawtransaction 02000000000101be3977a2761996cb71f48ee332846ac59d5402e1c37e709e6ae69db5c2ad39270000000000feffffff02ff76130000000000160014184337352372a5fa6218dd57ea1057305d39c77dd209efaf01000000160014c4520316f8d1dee0a19acde950d0540701d3d01302473044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e0121036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296ac8ec1f00

Check vout:1 in the results below:
{
  "txid": "122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c",
  "hash": "5fb52307d771c7dadf9f06006340a0acb5ff9ebe15d215d98f08e749d0ee2e59",
  "version": 2,
  "size": 222,
  "vsize": 141,
  "weight": 561,
  "locktime": 2092232,
  "vin": [
    {
      "txid": "2739adc2b59de66a9e707ec3e102549dc56a8432e38ef471cb961976a27739be",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "txinwitness": [
        "3044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e01",
        "036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296a"
      ],
      "sequence": 4294967294
    }
  ],
  "vout": [
    {
      "value": 0.01275647,
      "n": 0,
      "scriptPubKey": {
        "asm": "0 184337352372a5fa6218dd57ea1057305d39c77d",
        "hex": "0014184337352372a5fa6218dd57ea1057305d39c77d",
        "address": "tb1qrppnwdfrw2jl5cscm4t75yzhxpwnn3mamh6ff8",
        "type": "witness_v0_keyhash"
      }
    },
    {
      "value": 72.46645714,
      "n": 1,
      "scriptPubKey": {
        "asm": "0 c4520316f8d1dee0a19acde950d0540701d3d013",
        "hex": "0014c4520316f8d1dee0a19acde950d0540701d3d013",
        "address": "tb1qc3fqx9hc680wpgv6eh54p5z5quqa85qn35meep",
        "type": "witness_v0_keyhash"
      }
    }
  ]
}

We have all the information about inputs and outputs for this transaction similar to most of the block explorers:




Inputs
Outputs




tb1qc3fqx9hc680wpgv6eh54p5z5quqa85qn35meep (72.46645714)
tb1q9ndx8azqme4tskx0y4gd4uaj253dd3n0386xkn (0.01905871)



tb1qu9u6609s6w798ja9vnxzupdu9hqcmcdtvm87gx (72.44723495)




You can do everything using Bitcoin Core, JSON-RPC and PHP. Example:
<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'http://localhost:18222/',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{"jsonrpc": "1.0", "id": "curltest", "method": "getrawtransaction", "params": ["ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6"]}',
  CURLOPT_HTTPHEADER => array(
    'Authorization: Basic dXNlcjM6cGFzc3dvcmQz',
    'Content-Type: text/plain'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;


113470: <p>Currently im trying to build an explorer and im stuck on the block/listing transactions.</p>
<p>Currently this is my code.</p>
<pre><code>foreach($info['tx'] AS $key =&gt; $value){
            $txinfo = callBTC(&quot;getrawtransaction&quot;,array(&quot;\&quot;$value\&quot;&quot;,true));
            echo '&lt;pre&gt;';
            print_r($txinfo);
            echo '&lt;/pre&gt;';
            echo '&lt;div class=&quot;tx&quot;&gt;&lt;div class=&quot;txRow&quot;&gt;&lt;div class=&quot;txCat&quot;&gt;Hash&lt;/div&gt;&lt;div class=&quot;txInfo&quot;&gt;&lt;div class=&quot;overflow&quot;&gt;&lt;a href=&quot;index?txid='.$value.'&quot;&gt;'.$value.'&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;';
            echo '&lt;div class=&quot;txRow&quot;&gt;&lt;div class=&quot;txCat&quot;&gt;Time&lt;/div&gt;&lt;div class=&quot;txInfo&quot;&gt;'.date(&quot;Y-m-d H:i:s&quot;,$txinfo['time']).'&lt;/div&gt;&lt;/div&gt;';
            echo '&lt;div class=&quot;txRow&quot;&gt;&lt;div class=&quot;txCat&quot;&gt;From&lt;/div&gt;&lt;div class=&quot;txInfo&quot;&gt;&lt;div class=&quot;overflow&quot;&gt;';
            foreach($txinfo['vin'] AS $key2 =&gt; $value2){
                if(!empty($txinfo['vin'][$key2]['coinbase'])){
                    echo '&lt;div class=&quot;confirmed&quot;&gt;COINBASE - Newly Created Coins&lt;/div&gt;';
                }else{
                    $from = callBTC(&quot;getrawtransaction&quot;,array(&quot;\&quot;$value2[txid]\&quot;&quot;,true));
                    echo '&lt;pre&gt;';
                    print_r($from);
                    echo '&lt;/pre&gt;';
                    echo $from['vout'][0]['scriptPubKey']['addresses'][0].'&lt;br/&gt;';
                }
            }
            echo '&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;';
            echo '&lt;div class=&quot;txRow&quot;&gt;&lt;div class=&quot;txCat&quot;&gt;To&lt;/div&gt;&lt;div class=&quot;txInfo&quot;&gt;&lt;div class=&quot;overflow&quot;&gt;';
            foreach($txinfo['vout'] AS $key3 =&gt; $value3){
                if(!empty($value3['scriptPubKey']['addresses'])){
                    foreach($value3['scriptPubKey']['addresses'] AS $key4 =&gt; $value4){
                        echo '&lt;a href=&quot;index?addid='.$value4.'&quot;&gt;'.$value4.'&lt;/a&gt;&lt;br/&gt;';
                    }
                }
            }
            echo '&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;';
            echo '&lt;div class=&quot;txRow&quot;&gt;&lt;div class=&quot;txCat&quot;&gt;Fee&lt;/div&gt;&lt;div class=&quot;txInfo&quot;&gt;&lt;/div&gt;&lt;/div&gt;';
            echo '&lt;div class=&quot;txRow&quot;&gt;&lt;div class=&quot;txCat&quot;&gt;Amount&lt;/div&gt;&lt;div class=&quot;txInfo&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;';
        }
</code></pre>
<p>i got the to addresses fine but whne it comes to the from field i struggle to determine which is the valid address to use.</p>
<p>Lets use block 2092235 for example</p>
<p><a href="https://www.blockchain.com/btc-testnet/block/000000000000003f69d7c8cb4943df2d7a273883eb72024f3305122ed2dc7271?page=1" rel="nofollow noreferrer">https://www.blockchain.com/btc-testnet/block/000000000000003f69d7c8cb4943df2d7a273883eb72024f3305122ed2dc7271?page=1</a></p>
<p>I use blockchain.com to check against my site to ensure im displaying the proper data.</p>
<p>but when i spit out the transaction for the tx id ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6 i get this...</p>
<pre><code>Array
(
    [txid] =&gt; ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6
    [hash] =&gt; b388c750f3d15f1fcdb320fbc3398a65a34a3eb6933ca1738729f22a4a8a5924
    [version] =&gt; 2
    [size] =&gt; 222
    [vsize] =&gt; 141
    [weight] =&gt; 561
    [locktime] =&gt; 2092234
    [vin] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [txid] =&gt; 122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c
                    [vout] =&gt; 1
                    [scriptSig] =&gt; Array
                        (
                            [asm] =&gt; 
                            [hex] =&gt; 
                        )

                    [txinwitness] =&gt; Array
                        (
                            [0] =&gt; 304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401
                            [1] =&gt; 0258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280
                        )

                    [sequence] =&gt; 4294967294
                )

        )

    [vout] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [value] =&gt; 0.01905871
                    [n] =&gt; 0
                    [scriptPubKey] =&gt; Array
                        (
                            [asm] =&gt; 0 2cda63f440de6ab858cf2550daf3b25522d6c66f
                            [hex] =&gt; 00142cda63f440de6ab858cf2550daf3b25522d6c66f
                            [reqSigs] =&gt; 1
                            [type] =&gt; witness_v0_keyhash
                            [addresses] =&gt; Array
                                (
                                    [0] =&gt; tb1q9ndx8azqme4tskx0y4gd4uaj253dd3n0386xkn
                                )

                        )

                )

            [1] =&gt; Array
                (
                    [value] =&gt; 72.44723495
                    [n] =&gt; 1
                    [scriptPubKey] =&gt; Array
                        (
                            [asm] =&gt; 0 e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab
                            [hex] =&gt; 0014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab
                            [reqSigs] =&gt; 1
                            [type] =&gt; witness_v0_keyhash
                            [addresses] =&gt; Array
                                (
                                    [0] =&gt; tb1qu9u6609s6w798ja9vnxzupdu9hqcmcdtvm87gx
                                )

                        )

                )

        )

    [hex] =&gt; 020000000001015ced69c0d591aa8616d5be813dca34382be7ff86e8215d6c1dfb36226b912e120100000000feffffff02cf141d00000000001600142cda63f440de6ab858cf2550daf3b25522d6c66f27b5d1af01000000160014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab0247304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401210258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280caec1f00
    [blockhash] =&gt; 000000000000003f69d7c8cb4943df2d7a273883eb72024f3305122ed2dc7271
    [confirmations] =&gt; 1418
    [time] =&gt; 1631039838
    [blocktime] =&gt; 1631039838
)
</code></pre>
<p>then i get the tx id from the vin field to get info on the transaction for that address(the from field) and i get this.</p>
<pre><code>Array
(
    [txid] =&gt; 122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c
    [hash] =&gt; 5fb52307d771c7dadf9f06006340a0acb5ff9ebe15d215d98f08e749d0ee2e59
    [version] =&gt; 2
    [size] =&gt; 222
    [vsize] =&gt; 141
    [weight] =&gt; 561
    [locktime] =&gt; 2092232
    [vin] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [txid] =&gt; 2739adc2b59de66a9e707ec3e102549dc56a8432e38ef471cb961976a27739be
                    [vout] =&gt; 0
                    [scriptSig] =&gt; Array
                        (
                            [asm] =&gt; 
                            [hex] =&gt; 
                        )

                    [txinwitness] =&gt; Array
                        (
                            [0] =&gt; 3044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e01
                            [1] =&gt; 036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296a
                        )

                    [sequence] =&gt; 4294967294
                )

        )

    [vout] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [value] =&gt; 0.01275647
                    [n] =&gt; 0
                    [scriptPubKey] =&gt; Array
                        (
                            [asm] =&gt; 0 184337352372a5fa6218dd57ea1057305d39c77d
                            [hex] =&gt; 0014184337352372a5fa6218dd57ea1057305d39c77d
                            [reqSigs] =&gt; 1
                            [type] =&gt; witness_v0_keyhash
                            [addresses] =&gt; Array
                                (
                                    [0] =&gt; tb1qrppnwdfrw2jl5cscm4t75yzhxpwnn3mamh6ff8
                                )

                        )

                )

            [1] =&gt; Array
                (
                    [value] =&gt; 72.46645714
                    [n] =&gt; 1
                    [scriptPubKey] =&gt; Array
                        (
                            [asm] =&gt; 0 c4520316f8d1dee0a19acde950d0540701d3d013
                            [hex] =&gt; 0014c4520316f8d1dee0a19acde950d0540701d3d013
                            [reqSigs] =&gt; 1
                            [type] =&gt; witness_v0_keyhash
                            [addresses] =&gt; Array
                                (
                                    [0] =&gt; tb1qc3fqx9hc680wpgv6eh54p5z5quqa85qn35meep
                                )

                        )

                )

        )

    [hex] =&gt; 02000000000101be3977a2761996cb71f48ee332846ac59d5402e1c37e709e6ae69db5c2ad39270000000000feffffff02ff76130000000000160014184337352372a5fa6218dd57ea1057305d39c77dd209efaf01000000160014c4520316f8d1dee0a19acde950d0540701d3d01302473044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e0121036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296ac8ec1f00
    [blockhash] =&gt; 00000000000000d299298de1cfb14276667b7d55e8a1cf43c9dc825babadf058
    [confirmations] =&gt; 1420
    [time] =&gt; 1631039256
    [blocktime] =&gt; 1631039256
)
</code></pre>
<p>by looking at blockchain i know which address it SHOULD take (tb1qc3fqx9hc680wpgv6eh54p5z5quqa85qn35meep)...but when i use php and check for last key in array its not always the correct address in other transactions as sometimes the address i need is in a different index(same array tho just different index).</p>
<p>How can i programmatically determine the right address consistently?</p>
 -1: <blockquote>
<p>i got the to addresses fine but when it comes to the from field i struggle to determine which is the valid address to use.</p>
</blockquote>
<p>There is no 'from' address in any transaction as mentioned by Sipa in the comments.</p>
<p><a href="https://bitcoin.stackexchange.com/questions/106216/is-it-possible-to-assign-a-single-from-and-a-single-to-address-to-bitcoin-tr/">Is it possible to assign a single &quot;from&quot; and a single &quot;to&quot; address to Bitcoin transactions?</a></p>
<blockquote>
<p>but when i spit out the transaction for the tx id ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6</p>
</blockquote>
<blockquote>
<p>How can i programmatically determine the right address consistently?</p>
</blockquote>
<p>I will share steps to check inputs and outputs for this transaction using Bitcoin Core:</p>
<pre><code>$ bitcoin-cli getrawtransaction 0d45dacc0e50066a55c13b54cdcd6e365bb90be3a492c55bf4200b01d02fd748

</code></pre>
<pre><code>020000000001015ced69c0d591aa8616d5be813dca34382be7ff86e8215d6c1dfb36226b912e120100000000feffffff02cf141d00000000001600142cda63f440de6ab858cf2550daf3b25522d6c66f27b5d1af01000000160014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab0247304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401210258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280caec1f00
</code></pre>
<pre><code>$ bitcoin-cli decoderawtransaction 020000000001015ced69c0d591aa8616d5be813dca34382be7ff86e8215d6c1dfb36226b912e120100000000feffffff02cf141d00000000001600142cda63f440de6ab858cf2550daf3b25522d6c66f27b5d1af01000000160014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab0247304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401210258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280caec1f00
</code></pre>
<pre><code>{
  &quot;txid&quot;: &quot;ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6&quot;,
  &quot;hash&quot;: &quot;b388c750f3d15f1fcdb320fbc3398a65a34a3eb6933ca1738729f22a4a8a5924&quot;,
  &quot;version&quot;: 2,
  &quot;size&quot;: 222,
  &quot;vsize&quot;: 141,
  &quot;weight&quot;: 561,
  &quot;locktime&quot;: 2092234,
  &quot;vin&quot;: [
    {
      &quot;txid&quot;: &quot;122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c&quot;,
      &quot;vout&quot;: 1,
      &quot;scriptSig&quot;: {
        &quot;asm&quot;: &quot;&quot;,
        &quot;hex&quot;: &quot;&quot;
      },
      &quot;txinwitness&quot;: [
        &quot;304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401&quot;,
        &quot;0258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280&quot;
      ],
      &quot;sequence&quot;: 4294967294
    }
  ],
  &quot;vout&quot;: [
    {
      &quot;value&quot;: 0.01905871,
      &quot;n&quot;: 0,
      &quot;scriptPubKey&quot;: {
        &quot;asm&quot;: &quot;0 2cda63f440de6ab858cf2550daf3b25522d6c66f&quot;,
        &quot;hex&quot;: &quot;00142cda63f440de6ab858cf2550daf3b25522d6c66f&quot;,
        &quot;address&quot;: &quot;tb1q9ndx8azqme4tskx0y4gd4uaj253dd3n0386xkn&quot;,
        &quot;type&quot;: &quot;witness_v0_keyhash&quot;
      }
    },
    {
      &quot;value&quot;: 72.44723495,
      &quot;n&quot;: 1,
      &quot;scriptPubKey&quot;: {
        &quot;asm&quot;: &quot;0 e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab&quot;,
        &quot;hex&quot;: &quot;0014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab&quot;,
        &quot;address&quot;: &quot;tb1qu9u6609s6w798ja9vnxzupdu9hqcmcdtvm87gx&quot;,
        &quot;type&quot;: &quot;witness_v0_keyhash&quot;
      }
    }
  ]
}
</code></pre>
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th>Inputs</th>
<th>Outputs</th>
</tr>
</thead>
<tbody>
<tr>
<td>&quot;txid&quot;: &quot;122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c&quot;, &quot;vout&quot;: 1</td>
<td>tb1q9ndx8azqme4tskx0y4gd4uaj253dd3n0386xkn (0.01905871)</td>
</tr>
<tr>
<td></td>
<td>tb1qu9u6609s6w798ja9vnxzupdu9hqcmcdtvm87gx (72.44723495)</td>
</tr>
</tbody>
</table>
</div>
<p>To get more information about input being spent in this transaction:</p>
<pre><code>$ bitcoin-cli getrawtransaction 122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c

</code></pre>
<pre><code>02000000000101be3977a2761996cb71f48ee332846ac59d5402e1c37e709e6ae69db5c2ad39270000000000feffffff02ff76130000000000160014184337352372a5fa6218dd57ea1057305d39c77dd209efaf01000000160014c4520316f8d1dee0a19acde950d0540701d3d01302473044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e0121036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296ac8ec1f00
</code></pre>
<pre><code>$ bitcoin-cli decoderawtransaction 02000000000101be3977a2761996cb71f48ee332846ac59d5402e1c37e709e6ae69db5c2ad39270000000000feffffff02ff76130000000000160014184337352372a5fa6218dd57ea1057305d39c77dd209efaf01000000160014c4520316f8d1dee0a19acde950d0540701d3d01302473044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e0121036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296ac8ec1f00
</code></pre>
<p>Check <code>vout:1</code> in the results below:</p>
<pre><code>{
  &quot;txid&quot;: &quot;122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c&quot;,
  &quot;hash&quot;: &quot;5fb52307d771c7dadf9f06006340a0acb5ff9ebe15d215d98f08e749d0ee2e59&quot;,
  &quot;version&quot;: 2,
  &quot;size&quot;: 222,
  &quot;vsize&quot;: 141,
  &quot;weight&quot;: 561,
  &quot;locktime&quot;: 2092232,
  &quot;vin&quot;: [
    {
      &quot;txid&quot;: &quot;2739adc2b59de66a9e707ec3e102549dc56a8432e38ef471cb961976a27739be&quot;,
      &quot;vout&quot;: 0,
      &quot;scriptSig&quot;: {
        &quot;asm&quot;: &quot;&quot;,
        &quot;hex&quot;: &quot;&quot;
      },
      &quot;txinwitness&quot;: [
        &quot;3044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e01&quot;,
        &quot;036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296a&quot;
      ],
      &quot;sequence&quot;: 4294967294
    }
  ],
  &quot;vout&quot;: [
    {
      &quot;value&quot;: 0.01275647,
      &quot;n&quot;: 0,
      &quot;scriptPubKey&quot;: {
        &quot;asm&quot;: &quot;0 184337352372a5fa6218dd57ea1057305d39c77d&quot;,
        &quot;hex&quot;: &quot;0014184337352372a5fa6218dd57ea1057305d39c77d&quot;,
        &quot;address&quot;: &quot;tb1qrppnwdfrw2jl5cscm4t75yzhxpwnn3mamh6ff8&quot;,
        &quot;type&quot;: &quot;witness_v0_keyhash&quot;
      }
    },
    {
      &quot;value&quot;: 72.46645714,
      &quot;n&quot;: 1,
      &quot;scriptPubKey&quot;: {
        &quot;asm&quot;: &quot;0 c4520316f8d1dee0a19acde950d0540701d3d013&quot;,
        &quot;hex&quot;: &quot;0014c4520316f8d1dee0a19acde950d0540701d3d013&quot;,
        &quot;address&quot;: &quot;tb1qc3fqx9hc680wpgv6eh54p5z5quqa85qn35meep&quot;,
        &quot;type&quot;: &quot;witness_v0_keyhash&quot;
      }
    }
  ]
}
</code></pre>
<p>We have all the information about inputs and outputs for this transaction similar to most of the block explorers:</p>
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th>Inputs</th>
<th>Outputs</th>
</tr>
</thead>
<tbody>
<tr>
<td>tb1qc3fqx9hc680wpgv6eh54p5z5quqa85qn35meep (72.46645714)</td>
<td>tb1q9ndx8azqme4tskx0y4gd4uaj253dd3n0386xkn (0.01905871)</td>
</tr>
<tr>
<td></td>
<td>tb1qu9u6609s6w798ja9vnxzupdu9hqcmcdtvm87gx (72.44723495)</td>
</tr>
</tbody>
</table>
</div>
<p>You can do everything using Bitcoin Core, JSON-RPC and PHP. Example:</p>
<pre><code>&lt;?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL =&gt; 'http://localhost:18222/',
  CURLOPT_RETURNTRANSFER =&gt; true,
  CURLOPT_ENCODING =&gt; '',
  CURLOPT_MAXREDIRS =&gt; 10,
  CURLOPT_TIMEOUT =&gt; 0,
  CURLOPT_FOLLOWLOCATION =&gt; true,
  CURLOPT_HTTP_VERSION =&gt; CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST =&gt; 'POST',
  CURLOPT_POSTFIELDS =&gt;'{&quot;jsonrpc&quot;: &quot;1.0&quot;, &quot;id&quot;: &quot;curltest&quot;, &quot;method&quot;: &quot;getrawtransaction&quot;, &quot;params&quot;: [&quot;ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6&quot;]}',
  CURLOPT_HTTPHEADER =&gt; array(
    'Authorization: Basic dXNlcjM6cGFzc3dvcmQz',
    'Content-Type: text/plain'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
</code></pre>
