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
)
)
$txtinfoelement you are looping. For a good implement, you should define a$found=falsebefore 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 addecho $found. But there can be others issues, you should add a sample of the JSON received if you want to found an accurate answer.result,fromandtothat you are trying to access on it...array()function? It seems to serve no purpose... I mean, why not just compare$pawith$txtinfo['from']directly?