Jump to content

searching array


jmurch

Recommended Posts

I have an array with in my session array $_SESSION[discounts] :

[discounts] => Array
        (
            [0] => Array
                (
                    [0] => .30
                    [1] => 06200-08186
                )

            [1] => Array
                (
                    [0] => .30
                    [1] => 03016-11100
                )

            [2] => Array
                (
                    [0] => .30
                    [1] => 03015-00094
                )

            [3] => Array
                (
                    [0] => .30
                    [1] => 03015-00122
                )

I want to search through the array for the part number [1] and if it is in the array retrieve the discount [0].

 

TIA, Jeff

Link to comment
Share on other sites

I'd love to help and the theory of what your trying to do doesn't sound too complicated. But I'll be damned if I can work out what structure that array is based on what you posted. WHy do the .30 items not have a key? And are they the discount you are trying to fetch?

 

From what I would understand the logic array formation should be...

 

<?php
$search = // a part number
$discount = 0;
foreach($_SESSION['discounts'] as $dis) {
   if($dis[1] == $search) {
      $discount = $dis[0];
      break;
   }
}
?>

 

Obviously this code assumes that the key of the .30 part is 0. It may also make sense to convert this to a function that returns the value if found and returns 0 otherwise, but didn't want to complicate things any.

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.