0

I am pulling JSON data from etherscan and trying to see if an address exist in that transaction to see if it was sent or received.

what I want is to find all occurrences where the $pa is found in the from field and display them as "sent Transaction, hash = tx" and the same for the "to" field. So ultimately I will have a list of all the transactions and it will show if the transaction was sent or received. My current code is:

$pa = '0xE460c28E4cE2e4aE0B934a3eeb8FD88134d26956'
$url = ("http://api.etherscan.io/api?module=account&action=txlist&address=0xE460c28E4cE2e4aE0B934a3eeb8FD88134d26956&startblock=0&endblock=99999999&sort=asc&apikey=MY API KEY");

$content = file_get_contents($url);
$json = json_decode($content, true);
$data = $json['result'];

foreach ($data as $txinfo)
{
    $from =  array($txinfo['from'].',');
    $to = array($txinfo['to'].',');

    if (in_array($pa, $from))
    {
        echo 'found';
    }
    else
    {
        echo 'not found';
    }
}

But it doesn't find the address even though it does exist in array.. Where am I going wrong? When the array is created all the keys are [0].

Here is the JSON data that is returned -

Array ( [0] => 0x87fdc1fd91cb8a7db0021f1d749027b7048874cb, ) Array ( [0] => 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956, ) Array ( [0] => 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956, ) Array ( [0] => 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956, ) Array ( [0] => 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956, )

Here is the raw JSON data when I print_r($data)

Array (
    [0] => Array (
        [blockNumber] => 6625846
        [timeStamp] => 1541106828
        [hash] => 0xa35b44d7e2ff09f2c936d04ad53f472b3cae46b449ddd307b3bb0a9aa67aa7a0
        [nonce] => 18
        [blockHash] => 0x90cad8998ef5967f5a956d38a7cc37a5ffd00558d1d7e3bb93ae4fc9bae5d23e
        [transactionIndex] => 18
        [from] => 0x87fdc1fd91cb8a7db0021f1d749027b7048874cb
        [to] => 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956
        [value] => 56598736000000000
        [gas] => 21000
        [gasPrice] => 41000000000
        [isError] => 0 
        [txreceipt_status] => 1
        [input] => 0x
        [contractAddress] => 
        [cumulativeGasUsed] => 768594
        [gasUsed] => 21000
        [confirmations] => 5988
    )
    [1] => Array (
        [blockNumber] => 6631247
        [timeStamp] => 1541184437
        [hash] => 0x3f85168d763793e350b4273c531b055d37121b170a48675ba5decd80b62442d5
        [nonce] => 0
        [blockHash] => 0x9b6ff77963fd564d5c2beee5ce0117a0be23e9f30bb9561d931faa6fb4c1efa9
        [transactionIndex] => 8
        [from] => 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956
        [to] => 0x1faae436c85a15f257f7ff7f02af1e608815bbd7
        [value] => 0
        [gas] => 900000
        [gasPrice] => 40000000000
        [isError] => 0
        [txreceipt_status] => 1
        [input] => 0xa9059cbb000000000000000000000000129a92832d78cda2a89e9ea09cd688e4b692a93c000000000000000000000000000000000000000000000002b5e3af16b1880000
        [contractAddress] => 
        [cumulativeGasUsed] => 1221338
        [gasUsed] => 52200 
        [confirmations] => 587
    )
    [2] => Array (
        [blockNumber] => 6631287
        [timeStamp] => 1541184985
        [hash] => 0x621de75f0a2a880e17efc2c40a310223c38fa19b71b2dae375a9bc4d4d794a1a
        [nonce] => 1
        [blockHash] => 0x9c9433a4e22b107b0a9e84e2db5d4878d89e1f03730fd4cf3b7095f253418da8
        [transactionIndex] => 1
        [from] => 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956
        [to] => 0x1faae436c85a15f257f7ff7f02af1e608815bbd7
        [value] => 0
        [gas] => 900000
        [gasPrice] => 40000000000
        [isError] => 0
        [txreceipt_status] => 1
        [input] => 0xa9059cbb0000000000000000000000008336093a98fedc5df4e055a86c87fb79a68f64950000000000000000000000000000000000000000000000015af1d78b58c40000
        [contractAddress] =>
        [cumulativeGasUsed] => 73200
        [gasUsed] => 52200
        [confirmations] => 547
    )
    [3] => Array (
        [blockNumber] => 6631362
        [timeStamp] => 1541186019
        [hash] => 0x4a32e142b627a1541d0dc5605ef9c52b7f586f02ab502d13d0503321622ba435
        [nonce] => 2
        [blockHash] => 0x52d376055a2f73f20a2e3f0b7872403ee1d6a35c8903549116a975bd0c3d865f
        [transactionIndex] => 14
        [from] => 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956
        [to] => 0x1faae436c85a15f257f7ff7f02af1e608815bbd7
        [value] => 0
        [gas] => 900000
        [gasPrice] => 40000000000
        [isError] => 0
        [txreceipt_status] => 1
        [input] => 0xa9059cbb0000000000000000000000008336093a98fedc5df4e055a86c87fb79a68f64950000000000000000000000000000000000000000000000000de0b6b3a7640000
        [contractAddress] =>
        [cumulativeGasUsed] => 380279
        [gasUsed] => 37136
        [confirmations] => 472
    )
    [4] => Array (
        [blockNumber] => 6631368
        [timeStamp] => 1541186117
        [hash] => 0x0b40f67a334aae4cd54977e5e4cd5f54a2157f75441d5143d1eed4ba70f19b8f
        [nonce] => 3
        [blockHash] => 0x68981c00f3a7cff76938a852d9f4e96888ecee81ab51632764c86ce6b1ded910
        [transactionIndex] => 48
        [from] => 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956
        [to] => 0x1faae436c85a15f257f7ff7f02af1e608815bbd7
        [value] => 0 
        [gas] => 900000
        [gasPrice] => 40000000000
        [isError] => 0
        [txreceipt_status] => 1
        [input] => 0xa9059cbb0000000000000000000000008336093a98fedc5df4e055a86c87fb79a68f64950000000000000000000000000000000000000000000000000de0b6b3a7640000
        [contractAddress] =>
        [cumulativeGasUsed] => 1661953
        [gasUsed] => 37136
        [confirmations] => 466
    )
)
6
  • That loop you have made will echo a "found" or "not found" on every $txtinfo element you are looping. For a good implement, you should define a $found=false before entering the loop, then in the loop check this condition: if (in_array($pa, $from)) {$found=true; break;}. Now, at the end of the array add echo $found. But there can be others issues, you should add a sample of the JSON received if you want to found an accurate answer. Commented Nov 2, 2018 at 20:50
  • Ok thanks I will update the question, I am just doing found/not found until I can get the function working.. Commented Nov 2, 2018 at 20:51
  • You are sure the JSON you have pushed is like that, because i don't see the tags: result, from and to that you are trying to access on it... Commented Nov 2, 2018 at 20:56
  • yes, I will change it to the raw json data. That is returning just the "From" Commented Nov 2, 2018 at 20:57
  • 1
    Why do you use the array() function? It seems to serve no purpose... I mean, why not just compare $pa with $txtinfo['from'] directly? Commented Nov 2, 2018 at 21:04

2 Answers 2

1

I cut down your sample data to only contain the necessary parts. in_array() doesn't seem like the right tool for this job. Also, you will need case-insensitive matching OR you can more simply call strtolower() on $pa. By storing the respective matches in dedicated subarrays, you can simply output the data to the screen when the loop is finished.

Code: (Demo)

$data = [
    ['hash' => '0xa35b44d7e2ff09f2c936d04ad53f472b3cae46b449ddd307b3bb0a9aa67aa7a0',
     'from' => '0x87fdc1fd91cb8a7db0021f1d749027b7048874cb',
     'to' => '0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956'],
    ['hash' =>'0x3f85168d763793e350b4273c531b055d37121b170a48675ba5decd80b62442d5',
     'from' => '0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956',
     'to' => '0x1faae436c85a15f257f7ff7f02af1e608815bbd7'],
    ['hash' => '0x621de75f0a2a880e17efc2c40a310223c38fa19b71b2dae375a9bc4d4d794a1a',
     'from' => '0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956',
     'to' => '0x1faae436c85a15f257f7ff7f02af1e608815bbd7'],
    ['hash' => '0x4a32e142b627a1541d0dc5605ef9c52b7f586f02ab502d13d0503321622ba435',
     'from' => '0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956',
     'to' => '0x1faae436c85a15f257f7ff7f02af1e608815bbd7'],
    ['hash' => '0x0b40f67a334aae4cd54977e5e4cd5f54a2157f75441d5143d1eed4ba70f19b8f',
     'from' => '0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956',
     'to' => '0x1faae436c85a15f257f7ff7f02af1e608815bbd7']
];
$pa = strtolower('0xE460c28E4cE2e4aE0B934a3eeb8FD88134d26956');

$result = ['from' => [], 'to' => []];
foreach ($data as $set) {
    if ($pa == $set['from']) {
        $result['Sent'][] = $set['hash'];
    } elseif ($pa == $set['to']) {
        $result['Received'][] = $set['hash'];
    }
}

echo "For $pa\n";
echo "Sent Transactions:\n\t" , implode("\n\t", $result['Sent']) , "\n";
echo "Received Transactions:\n\t" , implode("\n\t", $result['Received']);

Output:

For 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956
Sent Transactions:
    0x3f85168d763793e350b4273c531b055d37121b170a48675ba5decd80b62442d5
    0x621de75f0a2a880e17efc2c40a310223c38fa19b71b2dae375a9bc4d4d794a1a
    0x4a32e142b627a1541d0dc5605ef9c52b7f586f02ab502d13d0503321622ba435
    0x0b40f67a334aae4cd54977e5e4cd5f54a2157f75441d5143d1eed4ba70f19b8f
Received Transactions:
    0xa35b44d7e2ff09f2c936d04ad53f472b3cae46b449ddd307b3bb0a9aa67aa7a0
Sign up to request clarification or add additional context in comments.

5 Comments

Holy cow thank you so much! you put a ton of effort into helping me and I truly appreciate it! It works just like I wanted cheers mate!!
It is my pleasure.
Is there a simple way to combine the list back in its regular succession so its order by date received/sent? Or should I ask that as a new question. Ive tried what I can think of but Im not sure the correct method to do this. Thanks again!
I don't mind making minor adjustments. What is your exact desired output? It may be easiest to send me your thoughts in a 3v4l.org link.
Wow thats so kind of you! Basically im looking for the list to be combined in its natural order rather than separated by "sent" and "received" I will try and get that link for you
0

You append a «,» to $from. That’s why your in_array() return false

EDIT:

$pa = '0xE460c28E4cE2e4aE0B934a3eeb8FD88134d26956'

[from] => 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956

In_array():

Note: If needle is a string, the comparison is done in a case-sensitive manner.

3 Comments

Im not sure im understanding what you are telling me
Try this: $from = array($txtinfo[‘from’]);
Please do a var_dump() on both $pa and $txtinfo[‘from’] inside the foreach-loop

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.