Skip to content

Instantly share code, notes, and snippets.

@stoimen
Last active February 6, 2016 11:10
Show Gist options
  • Save stoimen/02c44ea1d8238d5f39dc to your computer and use it in GitHub Desktop.
Save stoimen/02c44ea1d8238d5f39dc to your computer and use it in GitHub Desktop.
Sequential search
<?php
// unordered list
$arr = array(1, 2, 3, 3.14, 5, 4, 6, 9, 8);
// searched value
$x = 3.14;
$index = count($arr);
while ($arr[$index--] != $x);
echo "The value $x found on position " . ($index+1) . "!";
?>
<?php
// unordered list
$arr = array(1, 2, 3, 3.14, 5, 4, 6, 9, 8);
// searche value
$x = 3.14;
$arr[] = $x;
$index = 0;
while ($arr[$index++] != $x);
if ($index &lt; count($arr)) {
echo "The value $x found on position " . ($index - 1) . "!";
} else {
echo "The value $x not found!";
}
?>
<?php
// unordered list
$arr = array(1, 2, 3, 3.14, 5, 4, 6, 9, 8);
// searched value
$x = 3.14;
$index = null;
for ($i = 0; $i < count($arr); $i++) {
if ($arr[$i] == $x) {
$index = $i;
}
}
if (isset($index)) {
return true;
} else {
return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment