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
https://forums.phpfreaks.com/topic/176329-searching-array/
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
https://forums.phpfreaks.com/topic/176329-searching-array/#findComment-929372
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.