Jump to content

Recommended Posts

Let me explain my problem. I have an array with dates and numbers in format ($cronograma)

Ex:

Array ( [2020-09-21] => Array ( [0] => 2020-09-21 [1] => 2 [2] => 2 [3] => 2 ) [2020-09-28] => Array ( [0] => 2020-09-28 [1] => 2 [2] => 2 [3] => 4 )

Then i have another array with 2 ids (in this case 58,60) ($id)

Finally i have a third array with numbers only (in this case 34,34) $tot

So what i want is cross information beween them, for example for id 58 I must get dates (first element and last element when $tot = 34) for id 60 I must get dates (first element after $tot =34 and last element of array)

Whath i have so far is this

foreach ($id as $idPlan) { foreach ($cronograma as $c) { $t1 = 0; foreach ($tot as $d) { $t1 += (int)$d['tempos']; if ($c[3] == $t1) { $newAr[] =$idPlan; $newAr[] = $c[0]; } } } }

My response

array(8) { [0]=> string(2) "58" [1]=> string(10) "2021-02-01" [2]=> string(2) "58" [3]=> string(10) "2021-06-14" [4]=> string(2) "60" [5]=> string(10) "2021-02-01" [6]=> string(2) "60" [7]=> string(10) "2021-06-14" } null

So it's clear that i have all repeated

I should have a line like:

58 - 2020-09-21 -2021-02-01

Any help?

Link to comment
https://forums.phpfreaks.com/topic/311580-foreach-loop-issue-with-multiple-arrays/
Share on other sites

Where are you getting 2021-02-01 from? Where does the 34 from $tot come in?

How about this: given those three arrays,

$cronograma = array(
	"2020-09-21" => array("2020-09-21", 2, 2, 2),
	"2020-09-28" => array("2020-09-28", 2, 2, 4)
);

$id = array(
	58,
	60
);

$tot = array(
	34,
	34
);

(1) what is the output you want to get, and (2) what thought process did you follow for you to come up with that output?

21 minutes ago, requinix said:

Where are you getting 2021-02-01 from? Where does the 34 from $tot come in?

How about this: given those three arrays,


$cronograma = array(
	"2020-09-21" => array("2020-09-21", 2, 2, 2),
	"2020-09-28" => array("2020-09-28", 2, 2, 4)
);

$id = array(
	58,
	60
);

$tot = array(
	34,
	34
);

(1) what is the output you want to get, and (2) what thought process did you follow for you to come up with that output?

Hi

 

Thanks for your reply

 

To explain you i must tell you what a want to achieve

The first array ($cronograma) has information about dates,hours and total hours(last is incermented)

My full array have this data

2020-09-21 - 2 - 0 - 2
2020-09-22 - 2 - 0 - 2
2020-09-28 - 2 - 0 - 4
2020-09-29 - 2 - 0 - 4
2020-10-06 - 2 - 0 - 6
2020-10-12 - 2 - 0 - 6
2020-10-13 - 2 - 0 - 8
2020-10-19 - 2 - 0 - 8
2020-10-20 - 2 - 0 - 10
2020-10-26 - 2 - 0 - 10
2020-10-27 - 2 - 0 - 12
2020-11-02 - 2 - 0 - 12
2020-11-03 - 2 - 0 - 14
2020-11-09 - 2 - 0 - 14
2020-11-10 - 2 - 0 - 16
2020-11-16 - 2 - 0 - 16
2020-11-17 - 2 - 0 - 18
2020-11-23 - 2 - 0 - 18
2020-11-24 - 2 - 0 - 20
2020-11-30 - 2 - 0 - 20
2020-12-07 - 2 - 0 - 22
2020-12-14 - 2 - 0 - 24
2020-12-15 - 2 - 0 - 22
2021-01-04 - 2 - 0 - 26
2021-01-05 - 2 - 0 - 24
2021-01-11 - 2 - 0 - 28
2021-01-12 - 2 - 0 - 26
2021-01-18 - 2 - 0 - 30
2021-01-19 - 2 - 0 - 28
2021-01-25 - 2 - 0 - 32
2021-01-26 - 2 - 0 - 30
2021-02-01 - 2 - 0 - 34
2021-02-02 - 2 - 0 - 32
2021-02-08 - 2 - 0 - 36
2021-02-09 - 2 - 0 - 34
2021-02-22 - 2 - 0 - 38
2021-02-23 - 2 - 0 - 36
2021-03-01 - 2 - 0 - 40
2021-03-02 - 2 - 0 - 38
2021-03-08 - 2 - 0 - 42
2021-03-09 - 2 - 0 - 40
2021-03-15 - 2 - 0 - 44
2021-03-16 - 2 - 0 - 42
2021-03-22 - 2 - 0 - 46
2021-03-23 - 2 - 0 - 44
2021-04-05 - 2 - 0 - 48
2021-04-06 - 2 - 0 - 46
2021-04-12 - 2 - 0 - 50
2021-04-13 - 2 - 0 - 48
2021-04-19 - 2 - 0 - 52
2021-04-20 - 2 - 0 - 50
2021-04-26 - 2 - 0 - 54
2021-04-27 - 2 - 0 - 52
2021-05-03 - 2 - 0 - 56
2021-05-04 - 2 - 0 - 54
2021-05-10 - 2 - 0 - 58
2021-05-11 - 2 - 0 - 56
2021-05-17 - 2 - 0 - 60
2021-05-18 - 2 - 0 - 58
2021-05-24 - 2 - 0 - 62
2021-05-25 - 2 - 0 - 60
2021-05-31 - 2 - 0 - 64
2021-06-01 - 2 - 0 - 62
2021-06-07 - 2 - 0 - 66
2021-06-08 - 2 - 0 - 64
2021-06-14 - 2 - 0 - 68
2021-06-15 - 2 - 0 - 66
2021-06-22 - 2 - 0 - 68

 The second array " ids" is 2 differents school subjects (ex: Mat and English)

 

The third array is related to hours of each school subjects (ex: Mat and English), in this case 34 hours each 

 

So my main array start with date (when it starts until when its end (68 hours)

 

What i want is to slit this info

So, since i have 2 ids (58, 60)

id        stardate          endDate            total 

58     2020-09-21     2021-02-01        34

60    2021-02-08      2021-06-22        68

 

So i can insert into database those values

So i started to put foreach for array ids, inside foreach for array cronograma, and inside hours array

 

But the response given is not what i'm looking for

But how do you know which is which?

2020-09-21 - 2 - 0 - 2
2020-09-22 - 2 - 0 - 2
2020-09-28 - 2 - 0 - 4
2020-09-29 - 2 - 0 - 4

The 09-21 and 09-22 dates are for the two classes. The 09-28 and 09-29 dates are also for the two classes. But it isn't obvious that the 09-21 and 09-28 dates are the same class.
Is there anything other than the 7 day difference to tell which one goes for which class? What if a class is rescheduled to be a day earlier or later? I see one class will be missing two dates at the beginning of 2021-12 - aren't they going to be 4 hours behind the other?

I understand your questions and they are really important to avoid any problem.

In this case the most important is last dates because is when the subjetcs end. If they are rescheduled a note must be writen by the teacher, 

Rigth now, the most important is to separate subject by id getting the start date and the end date.

See the image with only one subject, now they ask me to have only one example like this with more than one subject

 

crongrama.png

In that chart, why is 2020-09-21 a 4? Shouldn't there be a 2 in 09-21 and another 2 in 09-22?

The way I see it, the first step is to split that array of dates for both classes into one array of dates for each class. So one array has 09-21, 09-28, 10-12, and the other has 09-22, 09-29, 10-06, and 10-13.
That also means each array's total increments exactly the way it should: the previous total + the current number of hours. 2, 4, 6, 8, and so on. No confusion about 2, 2, 4, 4, 6, 6 or 64, 68, 66, 68.

Each array will then have one and only one item in it with a total of 34 hours. And only one 68 hours.

From there I'm not sure because I don't know what the output is supposed to represent. Is it a "schedule" for someone to get 34 hours of each class, but they can only take one class at a time?

Yes, this chart belongs to a case with class with only 14 students and only one subject with 4 hours each time.

For instance, the school have classes with only 14 students so no need more than one group.

In this case or other similar there is 2 groups, and each group can have classes in different days (this case), that's why you see 2,2, 4,4, etc

The most important is to separate subject by id getting the start date and the end date because i can store into database the start and the end of each subject

So... I'm right, then? You need those 4 pairs of start/end dates?

What I said before about splitting the one array into two (one for each subject), then go through each array looking for the total=0 and total=34 dates and then for the total=34+time (that allows you to support any class duration instead of total=36 which only works for 2 hour classes) and total=68 dates.

 

8 minutes ago, requinix said:

So... I'm right, then? You need those 4 pairs of start/end dates?

What I said before about splitting the one array into two (one for each subject), then go through each array looking for the total=0 and total=34 dates and then for the total=34+time (that allows you to support any class duration instead of total=36 which only works for 2 hour classes) and total=68 dates.

Exactly

I'm trying to figure out how to do it...i'm lost

😄

What i have so far is this

 

 $newAr = array(); //array
        $id = explode(',',$idPlan); //string to array (subjects id)

        foreach ($id as $idPlan) {
            foreach ($tot as $t){ //array with hours
                $tempos = (int)$t['tempos'];
            }
            foreach($cronograma as $c){ //where i have all dates and hours per day
                if($c[3]<= $tempos){
                    $newAr[] = $idPlan;
                    $newAr[] = $c[0];
                }
            }
            print "<br>";
            echo "data: ".$idPlan;
            print "<br>";
            print_r($newAr);
        }

The result is this


dados: 58
Array ( [0] => 58 [1] => 2020-09-21 [2] => 58 [3] => 2020-09-28 [4] => 58 [5] => 2020-10-12 [6] => 58 [7] => 2020-10-19 [8] => 58 [9] => 2020-10-26 [10] => 58 [11] => 2020-11-02 [12] => 58 [13] => 2020-11-09 [14] => 58 [15] => 2020-11-16 [16] => 58 [17] => 2020-11-23 [18] => 58 [19] => 2020-11-30 [20] => 58 [21] => 2020-12-07 [22] => 58 [23] => 2020-12-14 [24] => 58 [25] => 2021-01-04 [26] => 58 [27] => 2021-01-11 [28] => 58 [29] => 2021-01-18 [30] => 58 [31] => 2021-01-25 [32] => 58 [33] => 2021-02-01 ) 
dados: 60
Array ( [0] => 58 [1] => 2020-09-21 [2] => 58 [3] => 2020-09-28 [4] => 58 [5] => 2020-10-12 [6] => 58 [7] => 2020-10-19 [8] => 58 [9] => 2020-10-26 [10] => 58 [11] => 2020-11-02 [12] => 58 [13] => 2020-11-09 [14] => 58 [15] => 2020-11-16 [16] => 58 [17] => 2020-11-23 [18] => 58 [19] => 2020-11-30 [20] => 58 [21] => 2020-12-07 [22] => 58 [23] => 2020-12-14 [24] => 58 [25] => 2021-01-04 [26] => 58 [27] => 2021-01-11 [28] => 58 [29] => 2021-01-18 [30] => 58 [31] => 2021-01-25 [32] => 58 [33] => 2021-02-01 [34] => 60 [35] => 2020-09-21 [36] => 60 [37] => 2020-09-28 [38] => 60 [39] => 2020-10-12 [40] => 60 [41] => 2020-10-19 [42] => 60 [43] => 2020-10-26 [44] => 60 [45] => 2020-11-02 [46] => 60 [47] => 2020-11-09 [48] => 60 [49] => 2020-11-16 [50] => 60 [51] => 2020-11-23 [52] => 60 [53] => 2020-11-30 [54] => 60 [55] => 2020-12-07 [56] => 60 [57] => 2020-12-14 [58] => 60 [59] => 2021-01-04 [60] => 60 [61] => 2021-01-11 [62] => 60 [63] => 2021-01-18 [64] => 60 [65] => 2021-01-25 [66] => 60 [67] => 2021-02-01 )

So, my code is changing id subject but is starts again in first array element

Iv'e checked and i know why, originaly hours array have 2 elements (34,34), so with this foreach

 foreach ($tot as $t){
                $tempos = (int)$t['tempos'];
            }

it is starting again with 0 until reaching 34 and no 68 like i pretend in second iteration

 

Whats wrong?

 

 

That's the same code from before, right?

The $cronograma array has data from both groups mixed into it. That means it will be hard to work with. What is the code you have to fill that array? Instead of putting everything into one single array, use multiple arrays. One array for each group. An array of arrays would be a good idea for that.

Hi

 

Iv'e create 5 differents arrays for each week day.

Usually there is only 2 students groups, but in some cases, i can have all the student class in one day or more + 2 others days for groups (class divided in 2)

 $cronograma = array();
    $shift1 = array();
    $shift2 = array();
    $shift3 = array();
    $shift4 = array();
    $shift5 = array();

So my main array is called cronograma, and the others are (by days week, monday,etc)

at the end i'm using array_merge jo merge non empty arrays

In the following example i have 2 week days with all class (Monday and Tuesday), T1 and T2 in array

And i have 2 groups shifts in another 2 days (wednesday, thursday), Shift3 and shift4

 

This is the produced final array

 

2020-10-19 - 1 - T1 - 1
2020-10-20 - 2 - T2 - 3
2020-10-26 - 1 - T1 - 4
2020-10-27 - 2 - T2 - 6
2020-11-02 - 1 - T1 - 7
2020-11-03 - 2 - T2 - 9
2020-11-09 - 1 - T1 - 10
2020-11-10 - 2 - T2 - 12
2020-11-16 - 1 - T1 - 13
2020-11-17 - 2 - T2 - 15
2020-11-23 - 1 - T1 - 16
2020-11-24 - 2 - T2 - 18
2020-11-30 - 1 - T1 - 19
2020-12-07 - 1 - T1 - 20
2020-12-14 - 1 - T1 - 21
2020-12-15 - 2 - T2 - 23
2021-01-04 - 1 - T1 - 24
2021-01-05 - 2 - T2 - 26
2021-01-11 - 1 - T1 - 27
2021-01-12 - 2 - T2 - 29
2021-01-18 - 1 - T1 - 30
2021-01-19 - 2 - T2 - 32
2021-01-25 - 1 - T1 - 33
2021-01-26 - 2 - T2 - 35
2021-02-01 - 1 - T1 - 36
2021-02-02 - 2 - T2 - 38
2021-02-08 - 1 - T1 - 39
2021-02-09 - 2 - T2 - 41
2021-02-22 - 1 - T1 - 42
2021-02-23 - 2 - T2 - 44
2021-03-01 - 1 - T1 - 45
2021-03-02 - 2 - T2 - 47
2021-03-08 - 1 - T1 - 48
2021-03-09 - 2 - T2 - 50
2021-03-15 - 1 - T1 - 51
2021-03-16 - 2 - T2 - 53
2021-03-22 - 1 - T1 - 54
2021-03-23 - 2 - T2 - 56
2021-04-05 - 1 - T1 - 57
2021-04-06 - 2 - T2 - 59
2021-04-12 - 1 - T1 - 60
2021-04-13 - 2 - T2 - 62
2021-04-19 - 1 - T1 - 63
2021-04-20 - 2 - T2 - 65
2021-04-26 - 1 - T1 - 66
2021-04-27 - 2 - T2 - 68
2021-05-03 - 1 - T1 - 69
2021-05-04 - 2 - T2 - 71
2021-05-10 - 1 - T1 - 72
2021-05-11 - 2 - T2 - 74
2021-05-17 - 1 - T1 - 75
2021-05-18 - 2 - T2 - 77
2021-05-24 - 1 - T1 - 78
2021-05-25 - 2 - T2 - 80
2021-05-31 - 1 - T1 - 81
2021-06-01 - 2 - T2 - 83
2021-06-07 - 1 - T1 - 84
2021-06-08 - 2 - T2 - 86
2021-06-14 - 1 - T1 - 87
2021-06-15 - 2 - T2 - 89
2021-06-21 - 1 - T1 - 90
2021-06-22 - 2 - T2 - 92
2021-06-28 - 1 - T1 - 93
2021-06-29 - 2 - T2 - 95
2021-07-05 - 1 - T1 - 96
2021-07-06 - 2 - T2 - 98
2021-07-12 - 1 - T1 - 99
2021-07-13 - 1 - T2 - 100
2020-10-21 - 5 - Shift3 - 5
2020-10-28 - 5 - Shift3 - 10
2020-11-04 - 5 - Shift3 - 15
2020-11-11 - 5 - Shift3 - 20
2020-11-18 - 5 - Shift3 - 25
2020-11-25 - 5 - Shift3 - 30
2020-12-02 - 5 - Shift3 - 35
2020-12-09 - 5 - Shift3 - 40
2020-12-16 - 5 - Shift3 - 45
2021-01-06 - 5 - Shift3 - 50
2021-01-13 - 5 - Shift3 - 55
2021-01-20 - 5 - Shift3 - 60
2021-01-27 - 5 - Shift3 - 65
2021-02-03 - 5 - Shift3 - 70
2021-02-10 - 5 - Shift3 - 75
2021-02-17 - 5 - Shift3 - 80
2021-02-24 - 5 - Shift3 - 85
2021-03-03 - 5 - Shift3 - 90
2021-03-10 - 5 - Shift3 - 95
2021-03-17 - 5 - Shift3 - 100
2021-03-24 - 5 - Shift3 - 105
2021-04-07 - 5 - Shift3 - 110
2021-04-14 - 5 - Shift3 - 115
2021-04-21 - 5 - Shift3 - 120
2021-04-28 - 5 - Shift3 - 125
2021-05-05 - 5 - Shift3 - 130
2021-05-12 - 5 - Shift3 - 135
2021-05-19 - 5 - Shift3 - 140
2021-05-26 - 5 - Shift3 - 145
2021-06-02 - 5 - Shift3 - 150
2021-06-09 - 5 - Shift3 - 155
2021-06-16 - 5 - Shift3 - 160
2021-06-23 - 5 - Shift3 - 165
2021-06-30 - 1 - Shift3 - 166
2020-10-22 - 5 - Shift4 - 5
2020-10-29 - 5 - Shift4 - 10
2020-11-05 - 5 - Shift4 - 15
2020-11-12 - 5 - Shift4 - 20
2020-11-19 - 5 - Shift4 - 25
2020-11-26 - 5 - Shift4 - 30
2020-12-03 - 5 - Shift4 - 35
2020-12-10 - 5 - Shift4 - 40
2020-12-17 - 5 - Shift4 - 45
2021-01-07 - 5 - Shift4 - 50
2021-01-14 - 5 - Shift4 - 55
2021-01-21 - 5 - Shift4 - 60
2021-01-28 - 5 - Shift4 - 65
2021-02-04 - 5 - Shift4 - 70
2021-02-11 - 5 - Shift4 - 75
2021-02-18 - 5 - Shift4 - 80
2021-02-25 - 5 - Shift4 - 85
2021-03-04 - 5 - Shift4 - 90
2021-03-11 - 5 - Shift4 - 95
2021-03-18 - 5 - Shift4 - 100
2021-04-08 - 5 - Shift4 - 105
2021-04-15 - 5 - Shift4 - 110
2021-04-22 - 5 - Shift4 - 115
2021-04-29 - 5 - Shift4 - 120
2021-05-06 - 5 - Shift4 - 125
2021-05-13 - 5 - Shift4 - 130
2021-05-20 - 5 - Shift4 - 135
2021-05-27 - 5 - Shift4 - 140
2021-06-03 - 5 - Shift4 - 145
2021-06-17 - 5 - Shift4 - 150
2021-06-24 - 5 - Shift4 - 155
2021-07-01 - 5 - Shift4 - 160
2021-07-08 - 5 - Shift4 - 165
2021-07-15 - 1 - Shift4 - 166

So T1 and T2 must have 100 hours total - done

Shift3 and shift4 must have 166 hours total - done

 

Now i must:

 each id(subject) apply 50 hours of t1 and t2

each id(subject apply 80 hours of Shift3 and shift4

 

So i need foreach to iterate between them

first is array of subject id

second is array subjects hours

and third is the main array

 

right?

 

I have been following this topic (I say "following" but that is an exaggeration) and I admit I totally clueless about

  • what the inputs to this process are
  • what the goal of the process is
  • what the rules are to achieve the goal and what the constraints are
  • what T1, T2, shift3, shift4 are

Apart from that...

Hi Barand

Thanks for helping

Let me explain all the process

Here, in Portugal, we have professional school for students who don't want to go to unervisity. The student have 3 years to finish this.

All subjects ( math , english, etc) have more than one module. I mean, Math have 10 modules, each one must have a grade to have a final  classification, Portuguese have 12 modules, English 6...and so on.

Every teacher must do a scheduler for all modules (The teacher choose the subject, then, the year (10,11,12),. After choosing the year the system print out wich modules he must give to students. After that he chooses the starting date, the days he gonna gives classes and the hours for each day. Finally the system will generate the scheduler for all year modules. 

What

57 minutes ago, Barand said:

I have been following this topic (I say "following" but that is an exaggeration) and I admit I totally clueless about

  • what the inputs to this process are
  • what the goal of the process is
  • what the rules are to achieve the goal and what the constraints are
  • what T1, T2, shift3, shift4 are

Apart from that...

About your questions

1. what the inputs to this process are? Inputs are subjects, year, starting date, days, hours

2. what the goal of the process is . The goal is create a scheduler

3. what the rules are to achieve the goal and what the constraints are. Rules are only defined the school year calendar and holidays

4. what T1, T2, shift3, shift4 are .

T1 is all classes for monday, T2 is all classes for tuesday,T3 is all classes for wednesday, etc

shift1 is for monday when the classes is divided in 2 (covid 19)

shift2 is for tuesday

shift3 is for wednesday

 

So imagine this scenario

I choose Math,year 10, the system gonna print 3 modules

Module 1 - 34 hours

Module 2 - 44 hours

Module 1 - 60 hours

The classes is divided in 2 groups, each one in diffetent day, suppose monday and wednesday

so

i use an array shift1 to have all classes dates for group 1 in monday

so i use an array shift3 to have all classes dates for group 2 in wedenesday

 

it is clear now or more information is needed?

Sorry about my english,

 

Thanks

 

 

37 minutes ago, Barand said:

Any min/max limits on hours/day/hours per week?

No, no limit

But usually a class don't have more than 7/8 hours week in specific subject(for instance, multimedia have in one subject with 8 hours per week, 3 hours with all student class + 5 hours each group(class divided in 2)

 

Sending my form to get full picture 

 

Regards

form.png

Edited by gmc1103
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.