Repeated Data in an Array

0

I have the following code:

$lineas="";
$fechaAnt="";

  for ($l=0; $l < count($dat); $l++) {  

    if($dat[$l][0]!=$fechaAnt || $fechaAnt==""){ //Si tu fecha es diferente a la anterior o es igual a vacio porque es la primera vez

        $lineas=$dat[$l][1]; //inicia lineas

    }else if($dat[$l][0]==$fechaAnt){ //si tu fecha es igual a la anterior

        $lineas=$lineas.",".$dat[$l][1]; //sigue concatenando

    }
    $fechaAnt=$dat[$l][0]; //Asignas valor a la fecha anterior que es la que acabas de pasar

    $hola[]= [$dat[$l][0],$lineas];
  }

where the $ dat =

array
0:["2016-11-09", 1]
1:["2016-11-09", 1]
2:["2016-11-15", 1]
3:["2016-11-16", 1]
4:["2016-11-16", 1]
5:["2016-11-17", 2]

What it does is that if it is the same date in this case $dat[$l][0] concatenates the values to me, and the problem is that it puts the date with the first data.

0:["2016-11-09", 1]      //<- Esto no debería estar 
1:["2016-11-09", "1,1"]  //<- Esto si debería estar 
2:["2016-11-15", 1]      //<- Esto si debería estar ya que solo hay un dato en ea fecha       
3:["2016-11-16", 1]    //<- Esto no debería estar 
4:["2016-11-16", "1,1"]   //<- Esto si debería estar 
5:["2016-11-17", 2] //<- Esto si debería estar ya que solo hay un dato en ea fecha 

Someone who can help me?

    
asked by Soldier 11.08.2017 в 18:01
source

2 answers

0

To solve the problem you pose, add the variable $repetidos that will be responsible for counting the dates that are repeated and the variable $hola I have moved from the site where the values were assigned. What is done is that when you get a date that matches the previous one in the position of 'lineas' the concatenation of $lineas is assigned.

$lineas="";
$fechaAnt="";
$repetidos=1; //agregado

for ($l=0; $l < count($dat); $l++) {  

if($dat[$l][0]!=$fechaAnt || $fechaAnt==""){ //Si tu fecha es diferente a la anterior o es igual a vacio porque es la primera vez

    $lineas=$dat[$l][1]; //inicia lineas
    $hola[]= [$dat[$l][0],$lineas]; //Se asigna la la fecha y las lineas

}else if($dat[$l][0]==$fechaAnt){ //si tu fecha es igual a la anterior

    $lineas=$lineas.",".$dat[$l][1]; //sigue concatenando
    $hola[$l-$repetidos][1]= $lineas; //Se asigna solo la variable $lineas
    $repetidos++;

}
$fechaAnt=$dat[$l][0]; //Asignas valor a la fecha anterior que es la que acabas de pasar

}

Link to Code Test

    
answered by 11.08.2017 / 18:57
source
0

a code easier I think that complicastess.

<?php
$data[] =  (["2016-11-09", 1]);
$data[] =  (["2016-11-09", 1]);
$data[] =  (["2016-11-15", 1]);
$data[] =  (["2016-11-16", 1]);
$data[] =  (["2016-11-16", 1]);
$data[] =  (["2016-11-17", 2]);


$final= array();
$total = count($data);
  for ($l=0; $l < $total ; $l++) {  
      if(!isset($final[$data[$l][0]])){
        $final[$data[$l][0]] = $data[$l][1];
      }else{
        $final[$data[$l][0]] .= ','.$data[$l][1];
      }

  }

foreach ($final as $key => $valor) {
    echo "para la fecha " . $key . " el valor es ". $valor ."<br/>"; 
}

the result thrown is:

para la fecha 2016-11-09 el valor es 1,1
para la fecha 2016-11-15 el valor es 1
para la fecha 2016-11-16 el valor es 1,1
para la fecha 2016-11-17 el valor es 2

demo code here

    
answered by 11.08.2017 в 19:52