For looping

1

I'm getting the values from the array and trying to pass it on, but it eventually goes 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 ...

    
asked by anonymous 29.08.2018 / 21:04

2 answers

3

Error

You are having 1,2,3,4,5... because you are printing $i (index) and not $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 check the reason for the error.

The last value of the array would always give an error, because since the array counter ( count ) does not consider 0 as value, then it would always have 1 more value at the end.

To fix, it is only to take the = in the comparison.

An example running:

$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>"; 
}
    
29.08.2018 / 21:42
2

count - Count 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 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 ideone

    
29.08.2018 / 21:16