Jump to content

Fitering an array


format
Go to solution Solved by cyberRobot,

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.