Alberto Schossow Alberto Schossow - 2 months ago 12
MySQL Question

How to save an array of dates read from mysql on php?

I have the following code to save save the data as an array for the next year, $info is read from sql:

$saved = []
for ($x = 0; $x <= 365; $x++) {
$d=strtotime("+$x days");
$cd = date("Y-m-d", $d);
foreach($info as $i){
if($i['date'] == $cd){
$saved[$cd] = $i;
}
}
}


it works, but when I have many data with the same date it overrides it, how can I differentiate it without ruining the structure of

Answer

try this:

$saved = []
for ($x = 0; $x <= 365; $x++) {
    $d=strtotime("+$x days");
    $cd = date("Y-m-d", $d);
    foreach($info as $i){
        if($i['date'] == $cd){  
            $saved[$cd][] = $i;
        }
    }
}

that way $saved[$cd] is being pushed. That way $saved['2016-10-03'] will give you 1 result if there is only one, or an array of N if there are N results.