1

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

1
  • 1
    You need another dimension.... Commented Oct 2, 2016 at 4:12

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

try this

$saved = array();
$dates = array();
// separate array, make array for dates
for ($x = 0; $x <= 365; $x++) {
  $d=strtotime("+$x days");
  $cd = date("Y-m-d", $d);
  $dates[$cd] = $cd;
}

foreach($info as $i){
  if(isset($dates[$i['date'])) {
        $saved[$i['date']][] = $i;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.