我正在从etherscan提取JSON数据,并尝试查看该事务中是否存在地址以查看其是否已发送或接收。
我想要的是查找所有从$字段中找到$ pa的地方,并将它们显示为“已发送事务,哈希= tx”,对于“至”字段则显示为相同。因此,最终我将获得所有交易的清单,并显示交易是已发送还是已接收。 我当前的代码是:
$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';
}
}
但是,即使它确实存在于数组中,也找不到地址。创建数组时,所有键均为[0]。
这是返回的JSON数据-
Array ( [0] => 0x87fdc1fd91cb8a7db0021f1d749027b7048874cb, ) Array ( [0] => 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956, ) Array ( [0] => 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956, ) Array ( [0] => 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956, ) Array ( [0] => 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956, )
这是我print_r($ data)时的原始JSON数据
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
)
)
答案 0 :(得分:1)
我将您的样本数据缩减为仅包含必要的部分。 in_array()
似乎不是完成这项工作的正确工具。另外,您将需要不区分大小写的匹配,或者可以更简单地在strtolower()
上调用$pa
。通过将各自的匹配项存储在专用的子数组中,您可以在循环完成后将数据简单地输出到屏幕上。
代码:(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']);
输出:
For 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956
Sent Transactions:
0x3f85168d763793e350b4273c531b055d37121b170a48675ba5decd80b62442d5
0x621de75f0a2a880e17efc2c40a310223c38fa19b71b2dae375a9bc4d4d794a1a
0x4a32e142b627a1541d0dc5605ef9c52b7f586f02ab502d13d0503321622ba435
0x0b40f67a334aae4cd54977e5e4cd5f54a2157f75441d5143d1eed4ba70f19b8f
Received Transactions:
0xa35b44d7e2ff09f2c936d04ad53f472b3cae46b449ddd307b3bb0a9aa67aa7a0
答案 1 :(得分:0)
您将«,»附加到$ from。这就是为什么您的in_array()返回false
的原因编辑:
$ pa ='0xE460c28E4cE2e4aE0B934a3eeb8FD88134d26956'
[from] => 0xe460c28e4ce2e4ae0b934a3eeb8fd88134d26956
In_array():
注意: 如果needle是字符串,则以区分大小写的方式进行比较。