从具有多个值 php 的数组中过滤
filter from array with multiple value php
$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、莱特币
过滤这个的最佳方法是什么!!!
现在问题清楚多了
您可以使用 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
来更改过滤字段
$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、莱特币 过滤这个的最佳方法是什么!!!
现在问题清楚多了
您可以使用 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