0

From the query, I get name year and month name and its rate. From the query some year have only 4 months. So how can I add other missing months with default rate 0.0 to following array?

$data = array();
foreach($result as $key => $val){
    $data[$val['name']][$val['year']][date('F', strtotime("2000-".$val['month']."-01"))]= $val['rate'];
}
3
  • Can you share a sample data of $result Commented Jan 3, 2019 at 9:15
  • @executable here is his sample data ^^ stackoverflow.com/questions/54004483/… Commented Jan 3, 2019 at 9:19
  • This question would be a lot simpler if you would just provide sample input and sample output. E.g. bpaste.net/raw/0de604fbd6ea Commented Jan 3, 2019 at 9:19

1 Answer 1

1
$data = array();
foreach($result as $key => $val){
    if(!isset($data[$val['name']][$val['year']])) {
        //Initialize year array
        $data[$val['name']][$val['year']] = array(
        "January" => "0.0","February" => "0.0","March" => "0.0","April" => "0.0","May" => "0.0","June" => "0.0","July" => "0.0","August" => "0.0","September" => "0.0","October" => "0.0","November" => "0.0","December" => "0.0"
        ); 
    }
    $data[$val['name']][$val['year']][date('F', strtotime("2000-".$val['month']."-01"))]= $val['rate'];
}

Initialize year array as given in above code

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

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.