bdean2020 Posted August 13, 2008 Share Posted August 13, 2008 I've come across a problem with my IF statement. It's giving a "false positive". In debugging the code I've been working with, I wrote this to isolate my error... for ($i=0;$i<90;$i++) { echo ($array[4] . " " . $multiArray[$i][0] . " "); if($multiArray[$i][0] == $array[4]) echo ("TRUE"); else echo ("FALSE"); echo ("<br />");}} The output was quite long, but even when the two numbers ($array[4] and $multiArray[$i][0]) were the same in the output, they came up as "FALSE" when they should have been "TRUE". I'd like a way to make the rest of my script execute when and only when these two array values are equal. Note: All of the above script undergoes a "foreach" loop which re-assigns $array (and $array[4]) each time it iterates. Is there a workaround or a fix for this? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/119494-solved-if-statement-false-positives/ Share on other sites More sharing options...
wildteen88 Posted August 13, 2008 Share Posted August 13, 2008 You'll need to post more code. What are you trying to do and explain whats stored in the arrays ($array an $multiarray) Quote Link to comment https://forums.phpfreaks.com/topic/119494-solved-if-statement-false-positives/#findComment-615599 Share on other sites More sharing options...
adam84 Posted August 13, 2008 Share Posted August 13, 2008 Unless we see what is being stored in the arrays, I do not think there is a whole lot we can do for you. Quote Link to comment https://forums.phpfreaks.com/topic/119494-solved-if-statement-false-positives/#findComment-615605 Share on other sites More sharing options...
bdean2020 Posted August 14, 2008 Author Share Posted August 14, 2008 This is the original code, before I altered for debugging... <?php $alines = file('data/conally.txt'); $wlines = fopen('data/convillage.txt', 'w'); $lines = file('data/convillages.txt'); for ($j = 0;$j<=90;$j++) { if (strlen($alines[$j])!=0) { $multiArray[$j] = split(",",$alines[$j]); } } if(!is_array($lines)) die("File could not be opened"); foreach($lines as $line) { list($i, $x, $y, $player, $ally) = explode(',', $line); if ($ally != 0) { $array = array($i, $x, $y, $player, $ally); $string = implode(",",$array); fwrite($wlines,$string); for ($i=0;$i<90;$i++) { echo ($array[4] . " " . $multiArray[$i][0] . " "); if($multiArray[$i][0] == $array[4]) { $astring = urlencode($multiArray[$i][1]); fwrite($wlines,","); fwrite($wlines,$astring); fwrite($wlines,"/n"); } } } } fclose($wlines); ?> These are examples of the data in the files I'm reading from... conally.txt 1,-S- 67,Zzzz.. 57,TURK 2,FUBAR 33,$!(K 3,Undead 4,XoO 5,AD 50,)( 6,DL 7,Heroes 8,Order 29,shnb 9,wow 10,~Hell~ 11,Navajo 12,??? 13,KAFKAS 14,~GoW~ 15,SPTS 16,GadDar 17,Dutch 21,*RD* 18,Nemesi 19,JoKeR 37,My Own 20,OrderE 51,TDZ 22,Stoned convillages.txt 1,509,501,3314421,60 2,504,490,2105724,12 3,493,488,390765,18 4,500,516,201797,24 5,488,496,3289318,24 6,492,514,3109061,1 7,504,491,3264760,25 8,491,493,3289318,24 9,507,508,1812155,24 10,489,503,1993723,27 11,498,509,3099920,1 12,496,487,590927,8 13,499,500,751018,2 14,512,504,3289318,24 15,493,507,2840779,18 16,514,499,962134,24 17,511,510,962134,24 18,498,512,202303,23 19,512,492,1933873,62 20,484,506,1812155,24 21,489,508,201797,24 22,505,504,1993723,27 23,506,506,962134,24 24,493,502,1993723,27 25,515,508,2220108,26 26,509,489,355337,24 27,485,496,3291312,8 28,497,512,201797,24 29,484,498,3289318,24 30,495,496,1993723,27 31,503,497,1812155,24 32,501,487,2734225,24 All in all, I wanted to, for each line in "convillages.txt", match the fifth ([4]) value to the correct first ([0]) value in "conally.txt" and add the second ([1]) value in "conally.txt" to the end of the line in "convillages.txt" and write all of that to a new file "convillage.txt". The problem is that it would either write none or all of the values in "conally.txt" and I'm trying to get it to write the only matching one. Quote Link to comment https://forums.phpfreaks.com/topic/119494-solved-if-statement-false-positives/#findComment-616172 Share on other sites More sharing options...
awpti Posted August 14, 2008 Share Posted August 14, 2008 Well, I fixed your script so it runs without errors. I'm still not 100% sure what you're trying to do. You explanation isn't terribly clear. Also, newline character is \n, not /n <?php $alines = file('data/conally.txt'); $wlines = fopen('data/convillage.txt', 'w'); $lines = file('data/convillages.txt'); $count = count($alines); for ($j = 0; $j < $count; ++$j): if (strlen($alines[$j]) != 0): $multiArray[$j] = split(",",$alines[$j]); endif; endfor; if(!is_array($lines)) { die("File could not be opened"); } foreach($lines as $line): var_dump($line); list($i, $x, $y, $player, $ally) = explode(',', $line); if ($ally != 0): $array = array($i, $x, $y, $player, $ally); $string = implode(",",$array); fwrite($wlines,$string); for ($i = 0; $i < $count; ++$i): echo ($array[4] . " " . $multiArray[$i][0] . " "); if($multiArray[$i][0] == $array[4]): $astring = urlencode($multiArray[$i][1]); fwrite($wlines,","); fwrite($wlines,$astring); fwrite($wlines,"\n"); endif; endfor; endif; endforeach; fclose($wlines); Quote Link to comment https://forums.phpfreaks.com/topic/119494-solved-if-statement-false-positives/#findComment-616190 Share on other sites More sharing options...
bdean2020 Posted August 14, 2008 Author Share Posted August 14, 2008 Thank you. What I've been trying to do was copy the second value of these arrays so that it appeared at the end of these arrays, but so that it doesn't alter the files it has to be written into a third file. Here's the catch... the first value of the first array has to match the fifth value of the second array for it to be transferred and my IF statement isn't providing the correct TRUE/FALSE values. As you can see in that third file, the 2nd value of the first set of arrays hasn't been transferred at all. That's after running the file located here - except now it just tells you when it's finished to save system resources. So even though the script is improved (thanks again), the IF statement still isn't working. I've tried it via a free web host (110mb.com) and using the latest version of xampp (which includes apache, php 5, etc). Quote Link to comment https://forums.phpfreaks.com/topic/119494-solved-if-statement-false-positives/#findComment-616205 Share on other sites More sharing options...
awpti Posted August 14, 2008 Share Posted August 14, 2008 The problem is that $array[4] contains a newline character. It'll never match that way, unless you strip the newline character or typecast it to int. Quote Link to comment https://forums.phpfreaks.com/topic/119494-solved-if-statement-false-positives/#findComment-616207 Share on other sites More sharing options...
awpti Posted August 14, 2008 Share Posted August 14, 2008 Fixed. <?php $alines = file('data/conally.txt'); $wlines = fopen('data/convillage.txt', 'w'); $lines = file('data/convillages.txt'); $count = count($alines); for ($j = 0; $j < $count; ++$j): if (strlen($alines[$j]) != 0): $multiArray[$j] = split(",",$alines[$j]); endif; endfor; if(!is_array($lines)) { die("File could not be opened"); } foreach($lines as $line): list($i, $x, $y, $player, $ally) = explode(',', $line); if ($ally != 0): $array = array($i, $x, $y, $player, $ally); $string = implode(",",$array); fwrite($wlines,$string); for ($i = 0; $i < $count; ++$i): $array_fixed = str_replace("\n", '', $array[4]); if($multiArray[$i][0] == $array_fixed): $astring = urlencode($multiArray[$i][1]); fwrite($wlines,","); fwrite($wlines,$astring); fwrite($wlines,"\n"); endif; endfor; endif; endforeach; fclose($wlines); Quote Link to comment https://forums.phpfreaks.com/topic/119494-solved-if-statement-false-positives/#findComment-616208 Share on other sites More sharing options...
bdean2020 Posted August 14, 2008 Author Share Posted August 14, 2008 Thank you very much Although the value has to be fixed earlier on (before it's first written to the file), I believe I can manage that. Quote Link to comment https://forums.phpfreaks.com/topic/119494-solved-if-statement-false-positives/#findComment-616211 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.