Jump to content

in_array ( )


westman

Recommended Posts

No, but you can use strpos.

if(strpos($the_random_num,$looking_for) !== false) {  $print_this = '5 is here!';}

Of course, if the string is "4,55,12" it's going to find the "5" in "55". You can split the string into an array with explode.

Link to comment
Share on other sites

I don't see the problem why it can't

<?php$a = array('1.10', 12.4, 1.13);if (in_array('12.4', $a, true)) {	echo "'12.4' found with strict check\n";}if (in_array(1.13, $a, true)) {	echo "1.13 found with strict check\n";}?>The above example will output:1.13 found with strict check

Update:Oops a string, not an array... Yeah you should use strpos for that.But btw why not make an array?

Link to comment
Share on other sites

ok so if i use...

$the_random_num = "3,7,12,33,43,47";$the_random_num_explode = explode(",", $the_random_num);if(in_array(1,$the_random_num_explode)) {$print_this .= '1 is here!';}else{$print_this .= '';}if(in_array(2,$the_random_num_explode)) {$print_this .= '2 is here!';}else{$print_this .= '';}if(in_array(3,$the_random_num_explode)) {$print_this .= '3 is here!';}else{$print_this .= '';}// it will go on like this for 70 numbers

will this work?i do test my scripts but it takes me hours to change my code ;)

Link to comment
Share on other sites

Why? create a random array numbers, sort then turn into string, then back into array, and check seventy times if the number is there? just print out the six number in the original array, i'm pretty sure you will get the same result

<?php $random_numbers = array();$total_num = 6;while ( count($random_numbers) < $total_num )	{	$current_rand_num = mt_rand(1,75);	if (!in_array($current_rand_num,$random_numbers))		{		$random_numbers[] = $current_rand_num; // creates an array of random numbers		}	} sort($random_numbers); $the_random_num=""; for($i=0;$i<count($random_numbers);$i++)	{$the_random_num.=$i < $total_num-1 ? $random_numbers[$i].', ' : $random_numbers[$i]; // turn rand numbers into string	}	echo $the_random_num.'<br />';  	$the_random_num_explode = explode(",", $the_random_num); /// now turn string back into array???????  echo 'You might not believe this, but the array turned into string of numbers, then back into array using explode has the SAME numbers as the original array, who\'d believe it hey<br />'; foreach($the_random_num_explode as $explode_rand_num){echo $explode_rand_num.' exploded array number is here<br />';}echo '<br />';foreach($random_numbers as $hi_remember_me){echo $hi_remember_me.' original array number is still here<br />';}    ?>

Link to comment
Share on other sites

if you wish to loop through 70+ numbers, might be best to use this

for($i=1;$i<=70;$i++){$found=false;foreach($random_numbers as $hi_remember_me){if($hi_remember_me==$i)    {    $found=true;break;    }}if($found)    {    echo $i.' original array number is still here<br />';    }else    {    echo $i.' is definitely not<br />';    }    }

Link to comment
Share on other sites

i need 6 random numbers so am using this...

$random_numbers = array();$total_num = 6;while ( count($random_numbers) < $total_num ){$current_rand_num = mt_rand(1,75);if (!in_array($current_rand_num,$random_numbers)){$random_numbers[] = $current_rand_num;}}sort($random_numbers);$the_random_num = "";for($i=0;$i<count($random_numbers);$i++){//$the_random_num.=$random_num.', ';$the_random_num.=$i < $total_num-1 ? $random_numbers[$i].', ' : $random_numbers[$i];}

then i need to see if the random numbers are in my DB, i have 75 numbers in my DB set up as$number1 = enum '0','1' (if 1 its true)$number2 = enum '0','1'$number3 = enum '0','1'$number4 = enum '0','1'and so on.. to find a mach in the DB, so fare i have this...

if(in_array("1",$the_random_num_explode)) {$number1 = "1";}else{$number1 = "0";}if(in_array("2",$the_random_num_explode)) {$number2 = "1";}else{$number2 = "0";}if(in_array("3",$the_random_num_explode)) {$number3 = "1";}else{$number3 = "0";}

and so on... then at the end ask the DB for a mach. how dose it look?

Link to comment
Share on other sites

in the end... i used...

$random_numbers = array();$total_num = 6;while ( count($random_numbers) < $total_num ){$current_rand_num = mt_rand(1,75);if (!in_array($current_rand_num,$random_numbers)){$random_numbers[] = $current_rand_num;}}sort($random_numbers);$the_random_num = "";for($i=0;$i<count($random_numbers);$i++){//$the_random_num.=$random_num.', ';$the_random_num.=$i < $total_num-1 ? $random_numbers[$i].', ' : $random_numbers[$i];}$the_random_num_explode = explode(",", $the_random_num);

then..

if(in_array("1",$the_random_num_explode)) {$number1 = "1";}else{$number1 = "0";}if(in_array("2",$the_random_num_explode)) {$number2 = "1";}else{$number2 = "0";}//// 75 of them in total

then ...

$sql = mysql_query("SELECT * FROM numbers WHERE number1 = '$number1' AND number2 = '$number2'  AND number3 = '$number3'// 75 in total

and it seams to work however i have never got a mach from the random number generater (i have less than 50 results in my DB).if i set the random number to one in the DB if will find it will no problem. can some one tell me if it is working ok?again i have never got a match from the random number generater.

Link to comment
Share on other sites

You shouldn't have to copy somehting over 75 times. Computers were invented exactly to prevent repetitive tasks. You can shorten the second part of your code to this:

$number = array();for($i = 1; $i <= 75; $i++) {  $number[$i] = intval( in_array(strval($i),$the_random_num_explode) );}

And the third part of your code to this:

$query = 'SELECT * FROM numbers WHERE '; $first = true;foreach($number as $key=>$value) {  if(!$first) {	$query .= 'AND ';  } else {	$first = false;  }  $query .= 'number' . $key. "='{$value}'";}mysql_query($query);

Link to comment
Share on other sites

Do you have 75 different fields in your database? What exactly is this program for? You will never manage to put a result for every single combination of 75 bits in your database. The amount of combinations is in the order of 10,000,000,000,000,000,000,000

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...