2

i want to use Cal-Heatmap to visualize data and my current sql result is returned as

[ { "dateStampSecs": "1499637600", "value": "1" } ]

but i want

[ { "1499637600" : 1 } ]

How do I stick them together?

I am using Codeigniter and below is my sql:

public function get_calData(){

        $this->db->select('unix_timestamp(STR_TO_DATE(date_start, "%Y-%m-%d")) dateStampSecs,count(*) as value');
        $this->db->from('calendar');
        $query = $this->db->get()->result();


        print json_encode($query,JSON_PRETTY_PRINT);
    }

4 Answers 4

2
$dateStampSecs = array_column($query, "dateStampSecs");
$value = array_column($query, "value");
$result = array_combine($dateStampSecs, $value);
print json_encode($result,JSON_PRETTY_PRINT);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is the one I was looking for.
0

you have to echo the result like following

echo json_encode($query);

exit();

Comments

0

Try like this

$finalData='';
foreach ($query as $data){
    $finalData[][$data['dateStampSecs']]=$data['value'];
}
$finalData=json_encode($finalData);
dd($query,$finalData);     

It will give output as

enter image description here

1 Comment

The SQL returns an array already, therefore decode doesn't work.
0

Hope it will work

 $query = json_decode($query ,true);
    $query = [$query['dateStampSecs']=>$query['value']];
    dd(json_encode($query));`

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.