Is going into loop

Asked

Viewed 78 times

1

I’m taking the values of the array and trying to pass the for, but it eventually getting into loop and bringing the values 1,2,3,4,5,6....

array_push($var, $linha['data']);
array_push($var, $linha['data_th']);
array_push($var, $linha['data_fl']);       

$var = array_unique($var); 

for($i = 0; $i <= $var; $i++){
    echo  "<td><div class='data'>'$i'</div></td>"; 
}

The result of print_r of $var is:

Array ( [0] => 30/04 [1] => 03/05 [2] => [3] => 01/05 [6] => 02/05 [10] => 07/05 [12] => 04/05 [16] => 09/05 [17] => 18/05 [18] => 05/05 [27] => 10/05 [28] => 14/05 [30] => 11/05 [31] => 15/05 [34] => 16/05 [40] => 17/05 [49] => 21/05 [53] => 04/06 [55] => 22/05 [65] => 26/05 [67] => 24/05 [73] => 28/05 [75] => 25/05 [77] => 18/06 [81] => 29/05 [84] => 30/05 [87] => 31/05 [91] => 05/06 [94] => 06/06 [96] => 01/06 [101] => 15/06 [103] => 08/06 [104] => 29/06 [108] => 07/06 [109] => 12/06 [112] => 11/06 [118] => 14/06 [119] => 28/06 [128] => 00/00 [133] => 22/06 [135] => 13/06 [142] => 19/06 [145] => 20/06 [164] => 06/07 [172] => 25/06 [174] => 21/06 [181] => 26/06 [190] => 27/06 [199] => 02/07 [217] => 04/07 [218] => 15/07 [259] => 09/07 [262] => 10/07 [267] => 05/07 [271] => 08/07 [291] => 11/07 [294] => 12/07 [300] => 13/07 )

It returns the following error:

Notice: Undefined offset: 4 in C: xampp htdocs... on line 398 Notice: Undefined offset: 5 in C: xampp htdocs... on line 398 Notice: Undefined offset: 6 in C: xampp htdocs... on line 398 Notice: Undefined offset: 7 in C: xampp htdocs... on line 398 ...

  • 1

    Would not be $i <= count($var)

  • We need to know the arrays structure $linha to be able to print in the loop.

2 answers

2

Count - Counts the elements of an array

    array_push($var, $linha['data']);
    array_push($var, $linha['data_th']);
    array_push($var, $linha['data_fl']);       

    $var = array_unique($var); 

    for($i = 0; $i <= count($var); $i++){
       echo  "<td><div class='data'>'$i'</div></td>"; 
    }

example in the ideone

If you want the keys/values

$var = array("laranja", "morango");

    array_push($var, "banana");
    array_push($var, "uva");
    array_push($var,"cereja");       

    $var = array_unique($var);

foreach( $var as $key => $value ) {
   //se quiser retornar só os valores, elimine $key  
   echo $key . " " . $value."\n";
}

example in the ideone

  • Ele retorna '0'&#xA;'1'&#xA;'2'&#xA;'3'&#xA;'4'&#xA;'5'&#xA;'6'&#xA;'7'&#xA;'8'&#xA;'9'&#xA;'10'&#xA;'11'&#xA;'12'&#xA;'13'&#xA;'14'&#xA;'15'&#xA;'16'&#xA;'17'&#xA;'18'&#xA;'19'&#xA;'20'&#xA;'21'&#xA;'22'&#xA;'23'&#xA;'24'&#xA;'25'&#xA;'26'&#xA;'27'&#xA;'28'&#xA;'29'&#xA;'30'&#xA;'31'&#xA;'32'&#xA;'33'&#xA;'34'&#xA;'35'&#xA;'36'&#xA;'37'&#xA;'38'&#xA;'39'&#xA;'40'&#xA;'41'&#xA;'42'&#xA;'43'&#xA;'44'&#xA;'45'&#xA;'46'&#xA;'47'&#xA;'48'&#xA;'49'&#xA;'50'&#xA;'51'&#xA;'52'&#xA;'53'&#xA;'54'&#xA;'55'&#xA;'56'&#xA;'57'&#xA;'58'&#xA;...

  • @Aprendizzz you saw an example I put in result in ideone?

  • Yes it returns the Dice and not the value

  • Yes, your original question was misspelled, inducing misinterpretation, since in the output you put $i, giving margin that wanted to print the indices

2


Error

You’re having 1,2,3,4,5... why are you printing $i (index) and not the $var (value).


Correcting

# junta todos arrays em um array único ($var)
array_push($var, $linha['data']);
array_push($var, $linha['data_th']);
array_push($var, $linha['data_fl']);       

# elimina duplicados
$var = array_unique($var);

# conta quantos registros tem no total
$regs = count($var);

# faz o loop
for($i = 0; $i <= $regs; $i++){
   echo  "<td><div class='data'>" . $var[$i] . "</div></td>"; 
}

edit1

.....
# elimina duplicados
$var = array_unique($var);

# faz o loop
foreach ($var as $v) {
  echo  "<td><div class='data'>" . $v . "</div></td>";
}

edit2

As I said, I would verify the reason for the error.

Always the last value of the array, it would give error, because as the array counter (count) does not consider the 0 as value, then it would always be with 1 more value at the end.

To correct, just take the = in comparison.

A working example:

$var = Array ( 0 => '30/04', 1 => '03/05', 2 => '01/05');
              
# elimina duplicados
$var = array_unique($var);

# conta quantos registros tem no total
$regs = count($var);

# faz o loop
for($i = 0; $i < $regs; $i++){
   echo  "<td><div class='data'>" . $var[$i] . "</div></td>"; 
}
  • It returns the Array array ( [0] => 30/04 [1] => 03/05 [2] => [3] => 01/05 [6] => 02/05 [10] => 07/05 [12] => 04/05 [16] => 09/05 [17] => 18/05 [18] => 05/05 [27] => 10/05 [28] => 14/05 [30] => 11/05 [31] => 15/05 [34] => 16/05 [40] => 17/05 [49] => 21/05 [53] => 04/06 [55] => 22/05 [65] => 26/05 [67] => 24/05] => 24/05 [73] => 28/05 [75] => 25/05 [77] => 18/06 [81] => 29/05 [84] => 30/05 [87] => 31/05 [91] => 05/06 [94] => 06/06 [96] => 01/06 [101] => 15/06 [103] => 08/06 [104] => 29/06 [108] => 07/06 [109] => 12/06 ...

  • Gives a print_r($var) after $var = array_unique($var); ... Go to "edit" and post in the answer the result in the question.

  • It was to edit the question, not the comment, but all right! rs... Dude, the way I did it was to have printed out. Paste the bug again

  • Dscp, I edited the question with the bug.

  • Test with 2nd example

  • It worked!!! Thank you very much partner for the great help and attention, Grateful.

  • I’ll check later on why it didn’t work for, because it’s correct, it should work too. But usually, whenever we work with arrays, we usually use the foreach.

  • @Leocaracciolo I edited to get it right, is it wrong? That’s why the -1?

  • was bad, sorry there, I swear I saw something else in the issue.

  • @Leocaracciolo Opa, tranquil Leo! I found it strange even asked in the chat! rs

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.