Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highmaps Graphics with Laravel5

Good morning ! I am preparing my database for use HighMaps with Laravel5, I receive this JSON form since Laravel.

[{"hc-key":"es-vi"},{"hc-key":"es-cs"},{"hc-key":"es-lo"},{"hc-key":"es-z"}]

Higchamps needs the following format:

 var data = [{ 'hc-key': 'es-pm', value: 0 },
                { 'hc-key': 'es-va', value: 1 },
                { 'hc-key': '', value: 52 }];

How could I send the format data that Highmaps need?

The sql in controller is this:

$provincias= DB::table('provincia')->lists('hc-key');
like image 768
jc1992 Avatar asked Nov 10 '22 12:11

jc1992


1 Answers

Since I'm not really sure where you would get the "values" you speak of, and, I'm assuming you know where those values come from, the easiest way I can come up with quickly is to use json_decode and json_encode.

$data = '[{"hc-key":"es-vi"},{"hc-key":"es-cs"},{"hc-key":"es-lo"},{"hc-key":"es-z"}]';
$data = json_decode($data);
foreach ($data as $item) {
    if ($item->{'hc-key'} == 'your_key_here') {
        $item->value = 'your_numeric_value_here';
    }
}
$data = json_encode($data);
like image 193
John Shipp Avatar answered Nov 14 '22 22:11

John Shipp