Jump to content

[SOLVED] Pull 1 Number from an array


phoenixx5

Recommended Posts

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

<?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].' ';
}
}
?>

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.