Jump to content

Foreach com múltiplo arrays


alphasil

Recommended Posts

Boa noite

 

Alguém poderia dar-me uma ajuda neste problema

Tenho um código que deveria funcionar de uma forma mas que não estou a conseguir os resultados desejados

Basicamente é para um cronograma, tenho 2 ids num array , e tenho um array de datas

 

  foreach ($id as $idPlan) {
                $dados = $this->getDadosPlanificacao( $idPlan );
                $idCpModulos = $dados[0]['idCpModulos'];
                $tempos = $dados[0]['tempos'];
                print_r($cronograma);
                foreach ($cronograma as $d => $values ){
                    if ($values[3] <= $tempos) {
                        $newArr[$d]['dataAula'] = $values[0];
                        $newArr[$d]['tempos'] = $values[1];
                        $newArr[$d]['tempos2'] = $values[2];
                        $newArr[$d]['total'] = $values[3];
                        print "<br>";
                        //echo "idPl:".$idPlan." - ".$values[0]." - ".$values[1]." - ".$values[3];
                    }
                }

                //$this->setShedulerByidPlan( $newArr, $endDates, $idPlan, $idCpModulos );
                $total = $tempos;
            }

O que se passa é que me está a repetir as datas na segunda iteração

Exemplo do primeiro id 

idPl:77 - 2020-10-26 - 2 - 2
idPl:77 - 2020-11-02 - 2 - 4
 

Exemplo do segundo id

idPl:78 - 2020-10-26 - 2 - 2
idPl:78 - 2020-11-02 - 2 - 4

 

Alguma dica?

 

Obrigado 

gmc11

 

Link to comment
Share on other sites

Bom dia

Sim está a retornar os valores corretos

A questão é que me está a voltar, na segunda iteração ao início do array cronograma, por isso repete as datas.

Já tentei no final da primeira iteração usar o array_slice para remover os registos adicionados no novo array para ficar somente com os restantes mas não me dá.

Este é o meu array principal

array(34) { ["2020-09-21"]=> array(4) { [0]=> string(10) "2020-09-21" [1]=> string(1) "2" [2]=> int(0) [3]=> int(2) } ["2020-09-28"]=> array(4) { [0]=> string(10) "2020-09-28" [1]=> string(1) "2" [2]=> int(0) [3]=> int(4) } ["2020-10-12"]=> array(4) { [0]=> string(10) "2020-10-12" [1]=> string(1) "2" [2]=> int(0) [3]=> int(6) } ["2020-10-19"]=> array(4) { [0]=> string(10) "2020-10-19" [1]=> string(1) "2" [2]=> int(0) [3]=> int(8) } ["2020-10-26"]=> array(4) { [0]=> string(10) "2020-10-26" [1]=> string(1) "2" [2]=> int(0) [3]=> int(10) } ["2020-11-02"]=> array(4) { [0]=> string(10) "2020-11-02" [1]=> string(1) "2" [2]=> int(0) [3]=> int(12) } ["2020-11-09"]=> array(4) { [0]=> string(10) "2020-11-09" [1]=> string(1) "2" [2]=> int(0) [3]=> int(14) } ["2020-11-16"]=> array(4) { [0]=> string(10) "2020-11-16" [1]=> string(1) "2" [2]=> int(0) [3]=> int(16) } ["2020-11-23"]=> array(4) { [0]=> string(10) "2020-11-23" [1]=> string(1) "2" [2]=> int(0) [3]=> int(18) } ["2020-11-30"]=> array(4) { [0]=> string(10) "2020-11-30" [1]=> string(1) "2" [2]=> int(0) [3]=> int(20) } ["2020-12-07"]=> array(4) { [0]=> string(10) "2020-12-07" [1]=> string(1) "2" [2]=> int(0) [3]=> int(22) } ["2020-12-14"]=> array(4) { [0]=> string(10) "2020-12-14" [1]=> string(1) "2" [2]=> int(0) [3]=> int(24) } ["2021-01-04"]=> array(4) { [0]=> string(10) "2021-01-04" [1]=> string(1) "2" [2]=> int(0) [3]=> int(26) } ["2021-01-11"]=> array(4) { [0]=> string(10) "2021-01-11" [1]=> string(1) "2" [2]=> int(0) [3]=> int(28) } ["2021-01-18"]=> array(4) { [0]=> string(10) "2021-01-18" [1]=> string(1) "2" [2]=> int(0) [3]=> int(30) } ["2021-01-25"]=> array(4) { [0]=> string(10) "2021-01-25" [1]=> string(1) "2" [2]=> int(0) [3]=> int(32) } ["2021-02-01"]=> array(4) { [0]=> string(10) "2021-02-01" [1]=> string(1) "2" [2]=> int(0) [3]=> int(34) } ["2021-02-08"]=> array(4) { [0]=> string(10) "2021-02-08" [1]=> string(1) "2" [2]=> int(0) [3]=> int(36) } ["2021-02-22"]=> array(4) { [0]=> string(10) "2021-02-22" [1]=> string(1) "2" [2]=> int(0) [3]=> int(38) } ["2021-03-01"]=> array(4) { [0]=> string(10) "2021-03-01" [1]=> string(1) "2" [2]=> int(0) [3]=> int(40) } ["2021-03-08"]=> array(4) { [0]=> string(10) "2021-03-08" [1]=> string(1) "2" [2]=> int(0) [3]=> int(42) } ["2021-03-15"]=> array(4) { [0]=> string(10) "2021-03-15" [1]=> string(1) "2" [2]=> int(0) [3]=> int(44) } ["2021-03-22"]=> array(4) { [0]=> string(10) "2021-03-22" [1]=> string(1) "2" [2]=> int(0) [3]=> int(46) } ["2021-04-05"]=> array(4) { [0]=> string(10) "2021-04-05" [1]=> string(1) "2" [2]=> int(0) [3]=> int(48) } ["2021-04-12"]=> array(4) { [0]=> string(10) "2021-04-12" [1]=> string(1) "2" [2]=> int(0) [3]=> int(50) } ["2021-04-19"]=> array(4) { [0]=> string(10) "2021-04-19" [1]=> string(1) "2" [2]=> int(0) [3]=> int(52) } ["2021-04-26"]=> array(4) { [0]=> string(10) "2021-04-26" [1]=> string(1) "2" [2]=> int(0) [3]=> int(54) } ["2021-05-03"]=> array(4) { [0]=> string(10) "2021-05-03" [1]=> string(1) "2" [2]=> int(0) [3]=> int(56) } ["2021-05-10"]=> array(4) { [0]=> string(10) "2021-05-10" [1]=> string(1) "2" [2]=> int(0) [3]=> int(58) } ["2021-05-17"]=> array(4) { [0]=> string(10) "2021-05-17" [1]=> string(1) "2" [2]=> int(0) [3]=> int(60) } ["2021-05-24"]=> array(4) { [0]=> string(10) "2021-05-24" [1]=> string(1) "2" [2]=> int(0) [3]=> int(62) } ["2021-05-31"]=> array(4) { [0]=> string(10) "2021-05-31" [1]=> string(1) "2" [2]=> int(0) [3]=> int(64) } ["2021-06-07"]=> array(4) { [0]=> string(10) "2021-06-07" [1]=> string(1) "2" [2]=> int(0) [3]=> int(66) } ["2021-06-14"]=> array(4) { [0]=> string(10) "2021-06-14" [1]=> string(1) "2" [2]=> int(0) [3]=> int(68) } }

Estou meio perdido 

gmc11

 

Link to comment
Share on other sites

Não sei se percebi bem, mas parece-me que essa função está a devolver sempre os mesmos registos e não os registos referentes ao idPlan.

É isso?

Podes sempre usar o in_array para verificar se um elemento já existe no array e só o adicionar caso ainda não exista.

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. 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.