$tick = file_get_contents('https://api.coinmarketcap.com/v2/ticker');
$data = json_decode($tick, TRUE);
var_dump($data);
通过此代码过滤时$ data的结果:
if($key == 1027)
$me=$value;
array (size=10)
'id' => int 1
'name' => string 'Bitcoin' (length=7)
'symbol' => string 'BTC' (length=3)
'website_slug' => string 'bitcoin' (length=7)
'rank' => int 1
'circulating_supply' => float 17271050
'total_supply' => float 17271050
'max_supply' => float 21000000
'quotes' =>
array (size=1)
'USD' =>
array (size=6)
'price' => float 6501.77307056
'volume_24h' => float 3202089755.7065
'market_cap' => float 112292447790
'percent_change_1h' => float 0.14
'percent_change_24h' => float -0.67
'percent_change_7d' => float 1.37
'last_updated' => int 1537115604
我需要此结果的数据用于插入数据库
name' => string 'Ethereum' (length=8)
'symbol' => string 'ETH' (length=3)
'circulating_supply' => float 101996833
'total_supply' => float 101996833
'max_supply' => null
'quotes' =>
array (size=1)
'USD' =>
array (size=6)
'price' => float 218.486594482
'volume_24h' => float 1511169511.1798
'market_cap' => float 22284940724
'percent_change_1h' => float 0.7
'percent_change_24h' => float -2.81
'percent_change_7d' => float 8.08
'last_updated' => int 1537116039
现在请运行此代码
$tick = file_get_contents('https://api.coinmarketcap.com/v2/ticker');
$data = json_decode($tick, TRUE);
var_dump($data);
此阵列中有100多个数字硬币,我不再需要3个thia阵列硬币 BTC,ETH,莱特币 什么是过滤此的最佳方法!!!
答案 0 :(得分:0)
现在问题更加清楚了。
您可以使用array_filter来获取。
考虑以下代码:
$tick = file_get_contents('https://api.coinmarketcap.com/v2/ticker');
$data = json_decode($tick, TRUE);
$symbols = array("BTC", "ETH", "LTC");
$filtered = array_filter($data['data'], function($elm) use ($symbols){
return (in_array($elm['symbol'], $symbols));
});
echo print_r($filtered, true);
我按符号进行了过滤,但是您可以通过将(in_array($elm['symbol'], $symbols))
更改为其他字段,然后将symbol
更改过滤器字段