phoenixx5 Posted April 7, 2007 Share Posted April 7, 2007 This should be an easy one, but I've been up working for 24 hours straight on a project and this one's escaping me. I'm trying to set an array of a series of 6 single or double digit part numbers and match it against a user input selection of part numbers. For example, if the user is looking for part numbers 10,9,43,7,6 from an item that has part numbers 10,14,16,31,43,50 then it would highlight that part numbers 10 and 43 are a match, but so far it's only highlighting 1. Like I said should be an easy one. Here's the code. <?php $selection1 = 10; $selection2 = 14; $selection3 = 6; $selection4 = 15; $selection5 = 2; $selection6 = 50; $selectionCount=0; $arr = array(10,14,16,31,43,50); for ($a = 0; $a < count($arr); $a++) { $total = 0; for ($b = 0; $b <= $a; $b++) { $total += $arr[$b]; } if ($total == $selection1) { $selectionCount++; echo "$selection1 is a match<br>"; } if ($total == $selection2) { $selectionCount++; echo "$selection2 is a match<br>"; } if ($total == $selection3) { $selectionCount++; echo "$selection3 is a match<br>"; } if ($total == $selection4) { $selectionCount++; echo "$selection4 is a match<br>"; } if ($total == $selection5) { $selectionCount++; echo "$selection5 is a match<br>"; } if ($total == $selection6) { $selectionCount++; echo "$selection6 is a match<br>"; } } echo "You Had $selectionCount numbers that matched the part numbers available."; ?> Link to comment https://forums.phpfreaks.com/topic/46027-solved-pull-1-number-from-an-array/ Share on other sites More sharing options...
esukf Posted April 7, 2007 Share Posted April 7, 2007 <?php $exist_parts = array(10,14,16,31,43,50); $selections = array(10, 9, 43, 7, 6); //loop through $exist_parts and check if value exist in $selections and then highlight the number if found $count = count($exist_parts); for ($i = 0; $i < $count; $i++) { if (in_array($exist_parts[$i], $selections)) { echo '<font color="red">'.$exist_parts[$i].'</font> '; } else { echo $exist_parts[$i].' '; } } ?> Link to comment https://forums.phpfreaks.com/topic/46027-solved-pull-1-number-from-an-array/#findComment-223663 Share on other sites More sharing options...
esukf Posted April 7, 2007 Share Posted April 7, 2007 oops double post Link to comment https://forums.phpfreaks.com/topic/46027-solved-pull-1-number-from-an-array/#findComment-223665 Share on other sites More sharing options...
phoenixx5 Posted April 7, 2007 Author Share Posted April 7, 2007 Major thanks.... fast code... should have used i++ to begin w/ Thanks! Link to comment https://forums.phpfreaks.com/topic/46027-solved-pull-1-number-from-an-array/#findComment-223666 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.