redarrow Posted September 28, 2009 Share Posted September 28, 2009 why dosent this work please. i no the array_dif but what wrong with this please? <?php $numbers=array("1","2","3","4","5"); $numbers_not_wanted=array("3","5"); for($i=0; $i<count($numbers); $numbers_not_wanted++, $i++){ if($numbers[$i]==$numbers_not_wanted[$i]) continue; echo $numbers[$i]; } ?> Link to comment https://forums.phpfreaks.com/topic/175820-solved-maths-madness/ Share on other sites More sharing options...
Alex Posted September 28, 2009 Share Posted September 28, 2009 I'm assuming you want something like this:? $numbers=array("1","2","3","4","5"); $numbers_not_wanted=array("3","5"); for($i = 0;$i < count($numbers);$i++) { if(in_array($numbers[$i], $numbers_not_wanted)) continue; echo $numbers[$i]; } Assuming I'm right in guessing (Because you didn't specify what you want..) the reason yours didn't work is because you were only comparing the current element of $numbers to one element of $numbers_not_wanted. If I'm completely wrong then tell me what you really wanted. Link to comment https://forums.phpfreaks.com/topic/175820-solved-maths-madness/#findComment-926451 Share on other sites More sharing options...
redarrow Posted September 28, 2009 Author Share Posted September 28, 2009 here a php.net example of continue.... <?php for ($i = 0; $i < 5; ++$i) { if ($i == 2) continue print "$i\n"; } ?> why didn't mine work i no the in_array way! why didn't mine work like php.net example please.... please exsplin in depth. the reason yours didn't work is because you were only comparing the current element of $numbers to one element of $numbers_not_wanted. Link to comment https://forums.phpfreaks.com/topic/175820-solved-maths-madness/#findComment-926458 Share on other sites More sharing options...
Mark Baker Posted September 28, 2009 Share Posted September 28, 2009 It doesn't work because you're looping through the array indexes, assuming that they're the same as the index values. $numbers=array("1","2","3","4","5"); gives array(0 => '1', 1 => '2', 2 => '3', 3 => '4', 4 => '5') $numbers_not_wanted=array("3","5"); gives array(0 => '3', 1 => '5') $i=0; $numbers_not_wanted (not even set in first pass of loop, defaults to 0) $numbers[0] = '1' $numbers_not_wanted[0] = '3' The two don't match $i=1; $numbers_not_wanted = 1; $numbers[1] = '2' $numbers_not_wanted[1] = '5' The two don't match $i=2; $numbers_not_wanted = 2; $numbers[2] = '3' $numbers_not_wanted[2] doesn't exist The two don't match etc. Link to comment https://forums.phpfreaks.com/topic/175820-solved-maths-madness/#findComment-926464 Share on other sites More sharing options...
Daniel0 Posted September 28, 2009 Share Posted September 28, 2009 You'd also (in this case) get three E_NOTICEs. Link to comment https://forums.phpfreaks.com/topic/175820-solved-maths-madness/#findComment-926465 Share on other sites More sharing options...
redarrow Posted September 28, 2009 Author Share Posted September 28, 2009 Thank you all ....... alexwd your a grate programmer, and good help to php freaks , but mate loads off people always want to no why your solution works, so why not just tell them there and then, ur solution in words to your answer off php, cheers. i no it a pain to type your solutions, but as a master i am sure, you can short and sweet. Link to comment https://forums.phpfreaks.com/topic/175820-solved-maths-madness/#findComment-926468 Share on other sites More sharing options...
Alex Posted September 28, 2009 Share Posted September 28, 2009 Sorry, I usually try to explain my solutions, as I was writing that I was about to go do something so I was kind of rushing. Link to comment https://forums.phpfreaks.com/topic/175820-solved-maths-madness/#findComment-926471 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.