How to filter the php array with multiple keys

I have an array with keys, I want to filter the array with multiple keys,

I want filter the array upload_wire,ticker,exchnage and Price and etc Here the array

[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] => [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] => [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] => [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 )

I Have tried with below code, Now I want filter with Multiple keys for expample, Exchange and Price, I want to filter OTC and NYSE

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');

Excepted Result

[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] => [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] => [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 )
12

1 Answer

You can change the structure you want to filter with

$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]); } }
}

Demo

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like