如何使用多个键过滤php数组
我有一个包含键的数组,我想过滤包含多个键的数组 我要过滤数组上传线,股票,汇率和价格等 这里是数组如何使用多个键过滤php数组,php,arrays,filter,key,Php,Arrays,Filter,Key,我有一个包含键的数组,我想过滤包含多个键的数组 我要过滤数组上传线,股票,汇率和价格等 这里是数组 [23] => Array ( [serial] => 236 [upload_wire] => iHub [ticker] => PJET [exchange] => OTC [price] => 0.0022
[23] => Array
(
[serial] => 236
[upload_wire] => iHub
[ticker] => PJET
[exchange] => OTC
[price] => 0.0022
[displayPrice] => $0.0022
[MarketCapFloat] => 1655072.6148
[MarketCapString] => 1.655M
[title] => WCVC Announces Continued EVERx CBD Sports Water Sales Growth
[TimeFound] => 11:19:26 PM EST
[DateFound] => Apr 29, 2019
[url] => https://ih.advfn.com/stock-market/USOTC/priority-aviation-inc-PJET/stock-news/79573129/wcvc-announces-continued-everx-cbd-sports-water-sa
[KeyWordsList] => {'Share': 30, 'Convert': 1, '$': 20, 'Data': 21, 'Order': 8, 'Right': 4, 'Operate': 5, 'Phase': 1, 'Result': 9, 'Agree': 2, 'Sale': 5, 'Select': 7, 'Grow': 6, 'Crypto': 5, 'Test': 8, 'Profit': 1, 'Develop': 1, 'Produce': 1, 'Make': 2, 'Enter': 5, 'Introduce': 1, 'Cannabis': 4, 'Partner': 4, 'New': 53, 'Increase': 1, 'Priority': 6, 'Research': 1, 'Made': 1}
[timestamp] => 1556597966
[currentPrice] => 0
[openPrice] => 0.0007
[previousClose] => 0.0007
[netChange] => 0.0000
[bid] => 0.0006
[bidSize] => 5020000
[ask] => 0.0008
[askSize] => 10000
[marketTire] => N
[cumulativeVolume] => 0
[otc_CumulativeVolume] => 2372789
),
[29] => Array
(
[serial] => 344
[upload_wire] => GNW
[ticker] => ANF
[exchange] => NYSE
[price] => 28.97
[displayPrice] => $28.97
[MarketCapFloat] => 1976000000.0
[MarketCapString] => 1.976B
[title] => Abercrombie & Fitch Introduces Limited-Edition Fierce Fragrance Bottle Featuring International Football Star Romelu Lukaku NYSE:ANF
[TimeFound] => 11:13:22 PM EST
[DateFound] => Apr 29, 2019
[url] => https://globenewswire.com/news-release/2019/04/29/1811281/0/en/Abercrombie-Fitch-Introduces-Limited-Edition-Fierce-Fragrance-Bottle-Featuring-International-Football-Star-Romelu-Lukaku.html
[KeyWordsList] => {'Share': 8, 'Reveal': 1, 'Platform': 1, 'Customer': 1, 'Launch': 3, 'Transaction': 1, 'Design': 1, '$': 49, 'Data': 5, 'Right': 2, 'Team': 1, 'Operate': 1, 'Result': 1, 'Worldwide': 1, 'Select': 14, 'Success': 2, 'Exclusive': 1, 'Sign': 3, 'Positive': 1, 'Test': 1, 'Sell': 2, 'Sharing': 1, 'Produce': 1, 'Make': 2, 'Enter': 1, 'Introduce': 4, 'Partner': 1, 'Strength': 4, 'Promise': 1, 'Distribute': 2, 'New': 25, 'License': 2, 'Made': 2}
[timestamp] => 1556597602
[currentPrice] => 0
[openPrice] => 14.73
[previousClose] => 14.61
[netChange] => 0.17
[bid] => 14.6
[bidSize] => 2
[ask] => 15.24
[askSize] => 1900
[marketTire] => A
[cumulativeVolume] => 5816473
[otc_CumulativeVolume] => 2760233
)
[30] => Array
(
[serial] => 275
[upload_wire] => GNWOTC
[ticker] => ATIXF
[exchange] => NASDAQ
[price] => 0.268
[displayPrice] => $0.268
[MarketCapFloat] => 18822118.916
[MarketCapString] => 18.822M
[title] => AnalytixInsight Reports Annual 2018 Financial Results TSX Venture Exchange:ALY
[TimeFound] => 11:13:17 PM EST
[DateFound] => Apr 29, 2019
[url] => https://globenewswire.com/news-release/2019/04/25/1810180/0/en/AnalytixInsight-Reports-Annual-2018-Financial-Results.html
[KeyWordsList] => {'Share': 7, 'Reduce': 1, 'Platform': 4, 'Strong': 1, 'Customer': 8, 'Launch': 4, 'Transaction': 4, 'Collaborate': 1, 'Design': 1, 'Secondary': 1, '$': 61, 'Improve': 3, 'Data': 8, 'Order': 1, 'Right': 1, 'Operate': 6, 'Result': 12, 'Worldwide': 3, 'Agree': 5, 'Select': 16, 'Success': 6, 'Grow': 3, 'Major': 1, 'Strategic': 2, 'Sign': 3, 'Merge': 1, 'Test': 1, 'Develop': 13, 'Provide': 7, 'Produce': 1, 'Make': 2, 'Enter': 5, 'Partner': 2, 'Promise': 1, 'Distribute': 8, 'New': 26, 'Increase': 4, 'License': 2, 'Research': 9, 'Client': 2, 'Achieve': 2, 'Made': 1}
[timestamp] => 1556597597
[currentPrice] => 0
[openPrice] => 0.2773
[previousClose] => 0.273
[netChange] => -0.02738
[bid] => 0.2627
[bidSize] => 2500
[ask] => 2.64
[askSize] => 100
[marketTire] => D
[cumulativeVolume] => 0
[otc_CumulativeVolume] => 900
)
我已经尝试了下面的代码,现在我想过滤器与expample,交易所和价格的多个键,我想过滤场外交易和纽约证券交易所
function filter_by_value ($array, $index, $value){
if(is_array($array) && count($array)>0)
{
foreach(array_keys($array) as $key){
$temp[$key] = $array[$key][$index];
if ($temp[$key] == $value){
$newarray[$key] = $array[$key];
}
}
}
return $newarray;
}
$nResult = filter_by_value ($nResult, 'exchange', 'OTC');
例外结果
[23] => Array
(
[serial] => 236
[upload_wire] => iHub
[ticker] => PJET
[exchange] => OTC
[price] => 0.0022
[displayPrice] => $0.0022
[MarketCapFloat] => 1655072.6148
[MarketCapString] => 1.655M
[title] => WCVC Announces Continued EVERx CBD Sports Water Sales Growth
[TimeFound] => 11:19:26 PM EST
[DateFound] => Apr 29, 2019
[url] => https://ih.advfn.com/stock-market/USOTC/priority-aviation-inc-PJET/stock-news/79573129/wcvc-announces-continued-everx-cbd-sports-water-sa
[KeyWordsList] => {'Share': 30, 'Convert': 1, '$': 20, 'Data': 21, 'Order': 8, 'Right': 4, 'Operate': 5, 'Phase': 1, 'Result': 9, 'Agree': 2, 'Sale': 5, 'Select': 7, 'Grow': 6, 'Crypto': 5, 'Test': 8, 'Profit': 1, 'Develop': 1, 'Produce': 1, 'Make': 2, 'Enter': 5, 'Introduce': 1, 'Cannabis': 4, 'Partner': 4, 'New': 53, 'Increase': 1, 'Priority': 6, 'Research': 1, 'Made': 1}
[timestamp] => 1556597966
[currentPrice] => 0
[openPrice] => 0.0007
[previousClose] => 0.0007
[netChange] => 0.0000
[bid] => 0.0006
[bidSize] => 5020000
[ask] => 0.0008
[askSize] => 10000
[marketTire] => N
[cumulativeVolume] => 0
[otc_CumulativeVolume] => 2372789
),
[29] => Array
(
[serial] => 344
[upload_wire] => GNW
[ticker] => ANF
[exchange] => NYSE
[price] => 28.97
[displayPrice] => $28.97
[MarketCapFloat] => 1976000000.0
[MarketCapString] => 1.976B
[title] => Abercrombie & Fitch Introduces Limited-Edition Fierce Fragrance Bottle Featuring International Football Star Romelu Lukaku NYSE:ANF
[TimeFound] => 11:13:22 PM EST
[DateFound] => Apr 29, 2019
[url] => https://globenewswire.com/news-release/2019/04/29/1811281/0/en/Abercrombie-Fitch-Introduces-Limited-Edition-Fierce-Fragrance-Bottle-Featuring-International-Football-Star-Romelu-Lukaku.html
[KeyWordsList] => {'Share': 8, 'Reveal': 1, 'Platform': 1, 'Customer': 1, 'Launch': 3, 'Transaction': 1, 'Design': 1, '$': 49, 'Data': 5, 'Right': 2, 'Team': 1, 'Operate': 1, 'Result': 1, 'Worldwide': 1, 'Select': 14, 'Success': 2, 'Exclusive': 1, 'Sign': 3, 'Positive': 1, 'Test': 1, 'Sell': 2, 'Sharing': 1, 'Produce': 1, 'Make': 2, 'Enter': 1, 'Introduce': 4, 'Partner': 1, 'Strength': 4, 'Promise': 1, 'Distribute': 2, 'New': 25, 'License': 2, 'Made': 2}
[timestamp] => 1556597602
[currentPrice] => 0
[openPrice] => 14.73
[previousClose] => 14.61
[netChange] => 0.17
[bid] => 14.6
[bidSize] => 2
[ask] => 15.24
[askSize] => 1900
[marketTire] => A
[cumulativeVolume] => 5816473
[otc_CumulativeVolume] => 2760233
)
您可以更改要用于筛选的结构
$allowedVals = ['exchange' => ["OTC", "NYSE"], 'price' => [28.97,0.268]];
$result = [];
foreach ($nResult as $key => $value) {
foreach ($allowedVals as $key1 => $value1) {
if (isset($value[$key1]) && count(array_intersect([$value[$key1]], $value1))) {
$result[$key] = $value;
}else{
unset($result[$key]);
}
}
}
如何过滤?删除项目?保管物品?是否根据关键字名称或值进行筛选?请编辑您的问题,以包括适当的解释,并向我们显示预期结果以及您当前的代码为您提供了什么。使用键和值筛选数组,数组包含筛选键和值所需的所有键,例如,我需要筛选EXchange OTC和NYSE,如例外结果所示,您将如何筛选价格?或者任何其他键值?@Rahul是的,我也想要过滤器价格,你能建议meAll filter的标准必须匹配,或者其中任何一个都应该匹配吗?检查一下我的答案!我修改了你的筛选条件数组。