Jump to content

[SOLVED] maths madness...


redarrow

Recommended Posts

 

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.