The Web Freaks .
PHP Freaks: PHP Help Forums -> in_array() incorrect! 2 == 57?! what?!
Sponsored By:
Support Our Advertisers!
.

PHP Freaks: PHP Help Forums -> in_array() incorrect! 2 == 57?! what?!

Search:
 
 
 
Documentation
 
 
Statistics
 

Hide Nav

 

PHP Freaks Forums


Welcome Guest ( Log In | Register )

Outline · Standard · [ Linear+ ]

> in_array() incorrect! 2 == 57?! what?!, in_array() does not work right

robodude666
post 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
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
 
Reply to this topicStart new topicStart Poll
Replies(1 - 2)
kenrbnsn
post 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.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Barand
post 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

user posted image
User is online!Profile CardPM
Go to the top of the page
+Quote Post

Reply to this topicTopic OptionsStart new topic
3 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
2 Members: beanfair, Barand
 

Lo-Fi Version Time is now: 1st October 2005 - 07:24 AM

 

 

 

PHP Function Lookup



PHPFreaks Hosted by:
Server Powered Dedcicated Servers
Dedicated Servers



Sponsors

PHP Web Hosting








NewsGator




PHP
XHTML
CSS2
Mozilla
Click here to kill your cookie!