| Sponsored By: |
# PHP Freaks: PHP Help Forums -> in_array() incorrect! 2 == 57?! what?! |
Search: |  |

PHPFreaks
PHP Articles PHP Freaks Tutorials User Submitted Tutorials PHP Scripts PHP Code Library PHP FAQ Latest Comments Live PHP Web Chat Member Search Privacy Policy Disclaimer Contact Us Donate to PHP Freaks
PHP Reference
Arrays Database Related Date and Time File Handling Image Manipulation Informational Mathematical Misc Functions Object Overloading Predefined Variables Session & Cookie Sockets System Commands Text Formatting PHP Manual
MySQL Reference
Column Types Functions Syntax MySQL Manual
Documentation
PHP Manual Smarty Manual PHP-GTK Manual PEAR Manual Apache Manual MySQL Manual PostgreSQL Manual JavaScript Manual HTML 4.01 Manual XHTML 1.0 Manual CSS Level 2 Manual
Forums
@phpfreaks.com Admin Cafe Apache Server Core PHP Hacking CSS (Style Sheets) Help Dreamweaver PHP Help Flat File Databases General Discussion HTML / Javascript / CSS HTML Help Javascript Help Microsoft IIS - PHP Microsoft SQL - MSSQL Miscellaneous MySQL Help Oracle Other Webservers PHP and MySQL PHP Discussions PHP Discussions PHP Forum Notes PHP Freaks Beta Tests PHP Freaks News PHP Freaks Syndication PHP Freelancing PHP General PHP Help PHP Installation PHP Newbie Help PHP Questions and Answers PHP SQL PHP Tutorial Help PHPFreaks 2.0 Polls PostgreSQL Site Administration SQL / Database Forums Third Party PHP Scripts Web Server Administration Website Critique
Statistics
Members: 44,753 Visitors Online: 1,825 Members Online: 5 Visits: 19,626,664 Members Online: deadonarrival Liquido Ripper_X GingerRobot tareq838
Super Sponsors Web Host Freaks - PHP Web Hosting Web Hosting Forums UK Domain Names - Low Cost Web Hosting - UK Web Design Virtuozzo VPS with FREE Plesk 7.5 | Hide Nav # PHP Freaks Forums PHP Freaks Board Guidelines Blogs Help Search Members Calendar Welcome Guest ( Log In | Register ) PHP Help Forums > PHP and MySQL > PHP Help Outline · Standard · [ Linear+ ] in_array() incorrect! 2 == 57?! what?!, in_array() does not work right Track this topic | Email this topic | Print this topic
robodude666 | Yesterday, 07:22 PM Post #1
n00bie Group: Members Posts: 3 Joined: 2-February 04 Member No.: 7,752 | Hello, I have killed the past 4 hours trying to get this to work. I have 2 arrays both creating using loops (foreach and while). After i do that i check to see if the numbers in one array are in the second. The script outputs that 1 == 45, or some other crazy combo. I was going crazy so i made a second code and it works fine!! I don't know what is wrong with my first one :( Please note: I did use a print_r to see if the arrays are being made correctly and they are. Please Help! Codes are below: First code: PHP $yours = array(); $rand = array(); $total = 0; foreach($_POST as $key => $value){ if(preg_match( "/^num(\d+)$/", $key, $match)){ if($_POST[$match[0]]){ $yours[] = $_POST[$match[0]]; $total++; } } } $i = 0; while($i < 10){ $temp = rand(1,80); if(!in_array($temp, $rand)){ $rand[] = $temp; $i++; } } sort($rand); sort($yours); $k = 0; while($k < 10){ if(in_array($yours[$k],$rand)){ $show1 .= "<b>".$rand[$k]."</b>, "; }else{ $show1 .= $rand[$k].", "; } if(in_array($rand[$k],$yours)){ $show2 .= "<b>".$yours[$k]."</b>, "; $wins++; }else{ $show2 .= $yours[$k].", "; } $k++; } $show1 = eregi_replace(", $", "", $show1); $show2 = eregi_replace(", $", "", $show2); echo $show1."<br />\n".$show2."<br />\nYou have {$wins} matching numbers!"; Second Code: PHP $yours = array(); $rand = array(); $total = 10; foreach($_POST as $key => $value){ if(preg_match( "/^num(\d+)$/", $key, $match)){ if($_POST[$match[0]]){ $yours[] = $_POST[$match[0]]; $total++; } } } $i = 0; while($i < 10){ $temp = rand(1,80); if(!in_array($temp, $rand)){ $rand[] = $temp; $i++; } } sort($rand); sort($yours); $k = 0; while($k < 10){ if(in_array($yours[$k],$rand)){ $show1 .= "<b>".$rand[$k]."</b>, "; }else{ $show1 .= $rand[$k].", "; } if(in_array($rand[$k],$yours)){ $show2 .= "<b>".$yours[$k]."</b>, "; $wins++; }else{ $show2 .= $yours[$k].", "; } $k++; } $show1 = eregi_replace(", $", "", $show1); $show2 = eregi_replace(", $", "", $show2); echo $show1."<br />\n".$show2."<br />\nYou have {$wins} matching numbers!"; Please help me, i dont understand why it won't work and its starting to piss me off. Thanks in advance, robodude666
Replies(1 - 2)
kenrbnsn | Today, 12:44 AM Post #2
Guru Group: Members Posts: 454 Joined: 7-September 05 Member No.: 24,079 | What is the input to these scripts? I notice you're using something from the $_POST array, but without knowing what it is, I won't be able to help you.
Barand | Today, 07:17 AM Post #3
Guru Group: Moderators Posts: 4,870 Joined: 25-June 03 From: Cheshire, UK Member No.: 3,113 | Does this help CODE $a = array (1, 2, 3, 4, 5, 6); $b = array (2, 4, 6, 8, 12, 15); $c = array_intersect($a, $b); echo 'You have ' . count($c) .' matching numbers : ' . join(', ', $c); -------------------- Barand baaGrid easy data tables - and more baaChart easy line, column and pie charts baaSelect generate js and php code for dynamic linked dropdowns
« Next Oldest · PHP Help · Next Newest »
3 User(s) are reading this topic (1 Guests and 0 Anonymous Users) 2 Members: beanfair, Barand Close Topic Options Track this topic Receive email notification when a reply has been made to this topic and you are not active on the board. Subscribe to this forum Receive email notification when a new topic is posted in this forum and you are not active on the board. Download / Print this Topic Download this topic in different formats or view a printer friendly version.
| Lo-Fi Version | Time is now: 1st October 2005 - 07:24 AM
Invision Power Board © 2005 IPS, Inc. Licensed to: PHP Freaks This Website © Copyright 2002-2005 - The Web Freaks, INC - PHPFreaks.com Topics, Discussions and Posted Content is Copyright of the respective authors. The Web Freaks, INC. does not claim any ownership of author written content. | PHP Function Lookup
PHPFreaks Hosted by: Dedicated Servers Sponsors Sponsor Listings: Payroll Reseller Hosting Logo Design Online Casino Scams Casinos slots gambling best online casinos Online Casinos Affiliates: Free Website Templates ISP Provider Soft32.com GUIStuff Asp, Vb, C# and .Net OpenSource Projects Guild Wars World of Warcraft devscripts Free Downloads Adobe Photoshop Tutorials Web Hosting PHPkitchen PHP Noise OxyScripts.com Codewalkers PHP Wholesale Directory Diaries and Blogs Free Scripts RSS / XML Feeds Articles Tutorials Code Library Forum Posts Discussions