2

I try to create a list with nested foreach loop. First loop is looping some numbers, second loop is looping dates. I want to write one number to one date. So there is a another function to check that. But the result is numbers writes on dates multiple times.

Out is something like that :

number 5 is on 2013.01.15;
number 5 is on 2013.01.16;
number 5 is on 2013.01.17;
number 6 is on 2013.01.15;
number 6 is on 2013.01.17;

The code :

function create_event($numbers,$available_dates) {
  foreach($numbers as $number) {
    foreach($avaliable_dates as $av_date) {

      $date_check= dateCheck($av_date,$number);

      if ($date_check == 0) {
        echo "number ".$number." is on ".$av_date;
        break;
      } else {
        $send_again[] = $number;
      }

    }
  }
  create_event($send_again,$avaliable_dates);
}

I think inside loop is not break.

7
  • Dear Yasin, please clarify your question and try to debug your application line by line before posting. Cheers, Commented Jan 15, 2013 at 15:49
  • @Bondye I try to use continue; but nothing changed. Commented Jan 15, 2013 at 15:51
  • @DídacPérez You could right, but I'm working on it like 3 or 4 hours and I didn't achieve. So I just ask. Commented Jan 15, 2013 at 15:53
  • Please clarify what your question is. Commented Jan 15, 2013 at 15:55
  • Keep in mind you are only breaking out of the inner loop, the available dates loop. If you want to break out of the outer one, you'll need to break outside of the inner loop. Commented Jan 15, 2013 at 15:56

2 Answers 2

5

Your break; should break inner foreach loop!
The only reason for such behavior I see is repeating numbers in you array!(E.g. $numers=array(5,5,5,6,6); )
Try to insert: $numbers=array_unique($numbers); before your outer foreach loop
If you need to break both loops(inner and outer) write break 2; instead of break;

Sign up to request clarification or add additional context in comments.

Comments

1

Can you check something like this:

function create_event($numbers,$available_dates) {
    foreach ($numbers as $number) {
        foreach ($available_dates as &$av_date) {
            if (dateCheck($av_date, $number) == 0) {
                unset($av_date);
                break;
            }
        }
    }
}

5 Comments

I tried like this first. But there is an issue here, this type of loop is not check all dates. Because number count is not equal with dates count.
OK. Then I need more explanation for your problem. Do you want for each number to find correct date (only one) or you want for each date to find just one number?
I want to assign each number to first available date. Then this date not use next time and number loop must be continue to next number.
Yes, this one is helped me to change my code and find to solution. Thanks a lot. I was unset the $av_date and the $number also. Then sent new arrays into its own. So, values of previous arrays extracted values of new arrays. Then new loop is start. I know it's a bit complicated and my expression is not very good. Thanks for your all respond.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.