jmurch Posted October 2, 2009 Share Posted October 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/176329-searching-array/ Share on other sites More sharing options...
cags Posted October 2, 2009 Share Posted October 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176329-searching-array/#findComment-929372 Share on other sites More sharing options...
.josh Posted October 3, 2009 Share Posted October 3, 2009 they do have a key. forum is parsing it as bbcode. I edited his post to wrap in code tags so you can see it. Quote Link to comment https://forums.phpfreaks.com/topic/176329-searching-array/#findComment-929414 Share on other sites More sharing options...
jmurch Posted October 3, 2009 Author Share Posted October 3, 2009 Thanks cags I'll give that a try. You were correct about the array structure. When I pasted into the posting the formattiing got messed up. I have not idea whre the [0]s went. Jeff Quote Link to comment https://forums.phpfreaks.com/topic/176329-searching-array/#findComment-929415 Share on other sites More sharing options...
jmurch Posted October 3, 2009 Author Share Posted October 3, 2009 Thanks CV............. I forgot about that now that you mention it. Jeff Quote Link to comment https://forums.phpfreaks.com/topic/176329-searching-array/#findComment-929417 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.