Convert to int string of an array

0

I have the following code:

$dat[] =  ["2016-11-09", 1];
$dat[] =  ["2016-11-09", 1];
$dat[] =  ["2016-11-15", 1];
$dat[] =  ["2016-11-16", 1];
$dat[] =  ["2016-11-16", 1];
$dat[] =  ["2016-11-17", 2];

$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

}

What it does is concatenate the numbers in case the date is repeated and when printing the JSON it throws something like this

echo json_encode($hola); // $hola es mi variable que tiene mi arreglo 

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

and I need it to be as follows,

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

that in the values that are concatenated, remove the "", the problem is that I do not know how to do it Can someone help me?

    
asked by Soldier 11.08.2017 в 23:01
source

2 answers

1

Performing the test in rextester, add the variable $cuantos is responsible for adding new positions to the $hola array when the dates are repeated and the variable $lineas

was removed
$dat[] =  ["2016-11-09", 1];
$dat[] =  ["2016-11-09", 1];
$dat[] =  ["2016-11-15", 1];
$dat[] =  ["2016-11-16", 1];
$dat[] =  ["2016-11-16", 1];
$dat[] =  ["2016-11-17", 2];

$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
      $cuantos = 2;  //se inicia en 2 que viene siendo la 3 posición del arreglo donde quiero agregar un nuevo entero

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

     $hola[$l-$repetidos][$cuantos]= $dat[$l][1]; //Se asigna solo la variable $lineas
     $repetidos++;
     $cuantos++;
  }
  $fechaAnt=$dat[$l][0]; //Asignas valor a la fecha anterior que es la que acabas de pasar
}

Hello Test

    
answered by 11.08.2017 / 23:41
source
1

In order for the $lineas to be added to the array, the only change you need to make is to modify the line:

$hola[$l-$repetidos][1]= $lineas; //Se asigna solo la variable $lineas

To this:

$hola[$l-$repetidos][]= $dat[$l][1]; // Agregamos al arreglo la nueva "linea"

Demo

    
answered by 11.08.2017 в 23:46