westman Posted February 6, 2012 Share Posted February 6, 2012 can i use in_array ( ) for a string like... $the_random_num = "1,2,3,4,5,6,";$looking_for = 5;if (in_array($looking_for, $the_random_num)) {$print_this = "5 is here!";} if not what can i use to find a number in string? Link to comment Share on other sites More sharing options...
Ingolme Posted February 6, 2012 Share Posted February 6, 2012 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 More sharing options...
davidb52 Posted February 6, 2012 Share Posted February 6, 2012 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 More sharing options...
westman Posted February 6, 2012 Author Share Posted February 6, 2012 so if i use $the_random_num = "1,2,3,4,5,6,";$the_random_num_explode = explode(",", $the_random_num); "$the_random_num_explode" is an array"$the_random_num" is a string is this right? Link to comment Share on other sites More sharing options...
Ingolme Posted February 6, 2012 Share Posted February 6, 2012 That should work. Why don't you test your code before asking if it's OK? Link to comment Share on other sites More sharing options...
westman Posted February 7, 2012 Author Share Posted February 7, 2012 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 More sharing options...
justsomeguy Posted February 7, 2012 Share Posted February 7, 2012 Quote the numbers that you're searching for, the first test may always come back true. Link to comment Share on other sites More sharing options...
westman Posted February 7, 2012 Author Share Posted February 7, 2012 what do you meen? if(in_array("1",$the_random_num_explode)) {$print_this .= '1 is here!';}else{$print_this .= '';}// insted ofif(in_array(1,$the_random_num_explode)) {$print_this .= '1 is here!';}else{$print_this .= '';} ? Link to comment Share on other sites More sharing options...
justsomeguy Posted February 7, 2012 Share Posted February 7, 2012 Right. When you explode the string you end up with an array of strings, not an array of numbers. So you should be comparing them with strings instead of numbers also. Link to comment Share on other sites More sharing options...
dsonesuk Posted February 7, 2012 Share Posted February 7, 2012 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 More sharing options...
dsonesuk Posted February 7, 2012 Share Posted February 7, 2012 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 More sharing options...
westman Posted February 8, 2012 Author Share Posted February 8, 2012 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 More sharing options...
westman Posted February 9, 2012 Author Share Posted February 9, 2012 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 More sharing options...
Ingolme Posted February 9, 2012 Share Posted February 9, 2012 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 More sharing options...
westman Posted February 9, 2012 Author Share Posted February 9, 2012 your code looks good, but i am not on your LV i am not to good in arrays, am just learning could you tel me if my code will work? Link to comment Share on other sites More sharing options...
Ingolme Posted February 9, 2012 Share Posted February 9, 2012 I didn't see any obvious mistakes in it. If you want to know if it would work, try testing it. Link to comment Share on other sites More sharing options...
westman Posted February 9, 2012 Author Share Posted February 9, 2012 i have tested it but i have less than 50 results in my DB (it could take days to find a mach) so i cant see if the random number generater is workinghowever when i set the random number to one in the DB it will find it with no problem. Link to comment Share on other sites More sharing options...
Ingolme Posted February 9, 2012 Share Posted February 9, 2012 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 More sharing options...
westman Posted February 9, 2012 Author Share Posted February 9, 2012 yes as...$number1 = enum '0','1' (if 1 its true)$number2 = enum '0','1'$number3 = enum '0','1'$number4 = enum '0','1' Link to comment Share on other sites More sharing options...
Ingolme Posted February 9, 2012 Share Posted February 9, 2012 I'm asking what the database is for. Link to comment Share on other sites More sharing options...
westman Posted February 9, 2012 Author Share Posted February 9, 2012 to store 75 numbers Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.