format Posted December 7, 2015 Share Posted December 7, 2015 Hi Guys, I got great help here a few years ago and I wonder if I can ask another related question. I am using the floowing to get the smallest quote from the array below: $minSLevel = min($SLevels); // get minimum value$minSLevel[0] = str_replace(',', '', $minSLevel[0]); Array ( [0] => Array ( [0] => 35.18 [1] => Royal London ) [1] => Array ( [0] => 34.18 [1] => Zurich Life ) [2] => Array ( [0] => 32.74 [1] => Friends First ) [3] => Array ( [0] => 39.13 [1] => Aviva ) [4] => Array ( [0] => 40.59 [1] => Irish Life ) [5] => Array ( [0] => 35.26 [1] => New Ireland ) ) Works great. Is it possible to filter the above to find value of [0] where [1] = Zurich Life Many thanks for any help given. Very much appreciated. Jon Quote Link to comment https://forums.phpfreaks.com/topic/299663-fitering-an-array/ Share on other sites More sharing options...
QuickOldCar Posted December 7, 2015 Share Posted December 7, 2015 If want to match or more accurately compare something you can loop through an array with a foreach or for loop using a comparison operator , can also use some regex with preg_match or preg_match_all for multiples Check if a specific value exists in the array using in_array true/false array_search can return the key of the array Quote Link to comment https://forums.phpfreaks.com/topic/299663-fitering-an-array/#findComment-1527624 Share on other sites More sharing options...
format Posted December 7, 2015 Author Share Posted December 7, 2015 Thanks for the quick answer, a few options to read and learn. Would you know which option above would get me going the quickest. I had a quick look at the documentation and could see how to get the value of [0] and matching [1]. I suspect I have some reading ahead of me, just not sure which methos above I should start with. Many thanks Jon Quote Link to comment https://forums.phpfreaks.com/topic/299663-fitering-an-array/#findComment-1527626 Share on other sites More sharing options...
QuickOldCar Posted December 7, 2015 Share Posted December 7, 2015 I suppose that will depend the rest of your code and what doing with it. Quote Link to comment https://forums.phpfreaks.com/topic/299663-fitering-an-array/#findComment-1527628 Share on other sites More sharing options...
format Posted December 7, 2015 Author Share Posted December 7, 2015 Hmmm,all I want is to find the Zurich Life entry and its matching quote. Thats it really. Sorry if not using correct terms etc. Quote Link to comment https://forums.phpfreaks.com/topic/299663-fitering-an-array/#findComment-1527630 Share on other sites More sharing options...
Solution cyberRobot Posted December 7, 2015 Solution Share Posted December 7, 2015 You could try something like this: $matchingIndex = ''; foreach($yourArray as $currIndex => $subArray) { if($subArray[1] == 'Zurich Life') { $matchingIndex = $currIndex; break; //break out of the loop, since you found Zurich Life } } Quote Link to comment https://forums.phpfreaks.com/topic/299663-fitering-an-array/#findComment-1527639 Share on other sites More sharing options...
Barand Posted December 7, 2015 Share Posted December 7, 2015 Reconfgure your array so it is more useful, like Array ( [Royal London] => 35.18 [Zurich Life] => 34.18 [Friends First] => 32.74 [Aviva] => 39.13 [Irish Life] => 40.59 [New Ireland] => 35.26 ) This will do it $data = array ( array ( 35.18 , 'Royal London' ) , array ( 34.18 , 'Zurich Life' ) , array ( 32.74 , 'Friends First' ) , array ( 39.13 , 'Aviva' ) , array ( 40.59 , 'Irish Life' ) , array ( 35.26 , 'New Ireland' ) ); // reconfigure $companies = array_column($data,1); $prices = array_column($data,0); $newdata = array_combine($companies, $prices); echo $newdata['Zurich Life']; //--> 34.18 1 Quote Link to comment https://forums.phpfreaks.com/topic/299663-fitering-an-array/#findComment-1527640 Share on other sites More sharing options...
format Posted December 7, 2015 Author Share Posted December 7, 2015 Many thanks to you all for your replies. Lots to take in. cyberRobot, I tried your suggestion and it worked a charm. Barrand, your suggetion also make sperfect sence. Thank you for this. Good help like this is worth its weight in gold, especialy when on the back foot with time. Thanks again. Jon Quote Link to comment https://forums.phpfreaks.com/topic/299663-fitering-an-array/#findComment-1527644 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.