[JavaScript] [PHP] Write string to a config file, should be formatted as an Array

Hi All,

What i want to do is the following,
i want to write my config back to the configfile (smartmirror project with a configpage)
commented part is where my string is written back via send.php.
What is missing? the Array_Keys at item 4 (URL2)
And also the brackets inbetween },{

I hope that there is someone who can assist me.
(By the way, Openhab-Addon is planned)
Some links are shortened (full code on Github)
Link to the Repo

Config.js

//source
calendars: [
 {url: "URL1",color: "#ffffff",slice: "99"},
{url: "URL2",color: "#00ff00",slice: "99"} ],

//written back
//calendars: [
//{url: "URL1",color: "#ffffff",slice: "99",: "URL2",: "#00ff00",: "99}],

send.php

$a=array("url"=>"","color"=>"","slice"=>"");
$calendar['feed'] = "{" . implode('",', array_map(function ($v, $k) { return $k . ': "' . $v; }, $_GET['calfeed'], array_keys($a))) . "}";

Hi,

In your php seems you are missing the " around the {

“{” should probably work.

Cheers
Karsten

Well, the unquoted { and } are part of the function. I can not add a quote.

$calkeys=array("url"=>"","color"=>"","slice"=>"");
$elements = array();
foreach (array_chunk($_GET['calfeed'], 3) as $section) {
$elements[] = implode('",', array_map(function ($v, $k) { return $k . ': "' . $v; }, $section, array_keys($calkeys)));
}
$calendar['feed'] = "{" . implode( '"},'."\n\t\t\t".'{'  ,$elements) . '"}';

Is my approach now. It works like expected. Linefeed has been added. I’ll upload it on github soon.
Only adding more calendar Feeds will cause some problems at the moment.

ok - cool - as what was missing in the output had been the {

good it works now.