Please, looking for little push over my problem. Having this script which append data to my existing JSON. What I am looking is: when I append new data it is create new $key (category) and set it all to JSON. Better to show on example. This is my code:
function parseTag($content1,$tg)
{
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($content1);
libxml_clear_errors();
$attr = array();
foreach ($dom->getElementsByTagName($tg) as $tag) {
foreach ($tag->attributes as $attribName => $attribNodeVal)
{
$attr[$attribName]=$tag->getAttribute($attribName);
}
}
return $attr;
}
for ($i = 0; $i < count($split); $i++)
{
$attrib_arr = parseTag($split[$i],'path');
if (empty($attrib_arr)) {break;}
$data_results = file_get_contents('newfile.json');
$tempArray = json_decode($data_results, true);
$a = "a".$i;
$tempArray[]=array($a => $attrib_arr);
$jsonData = json_encode($tempArray, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
file_put_contents('newfile.json', $jsonData);
}
It output this:
[
{
"a0": {
"d": "m34.511 143.4v3.0302l54.101-0.63086v-2.078z",
"points": "34.510773,146.42924 88.611514,145.79838 88.611514,143.72037 34.510773,143.39908 ",
"fill": "#353564"
}
},
{
"a1": {
"d": "m34.511 146.43 78.119 2.1017 56.193-2.4552-80.211-0.27738z",
"points": "112.6299,148.53093 168.82266,146.07576 88.611514,145.79838 34.510773,146.42924 ",
"fill": "#afafde"
}
},
{
"a2": {
"d": "m88.612 143.72 80.211-1.5957v3.9511l-80.211-0.27738z",
"points": "168.82266,142.1247 168.82266,146.07576 88.611514,145.79838 88.611514,143.72037 ",
"fill": "#e9e9ff"
}
}
]
What I am looking for is this:
[
{
"x": {
"a0": {
"d": "m34.511 143.4v3.0302l54.101-0.63086v-2.078z",
"points": "34.510773,146.42924 88.611514,145.79838 88.611514,143.72037 34.510773,143.39908 ",
"fill": "#353564"
},
"a1": {
"d": "m34.511 146.43 78.119 2.1017 56.193-2.4552-80.211-0.27738z",
"points": "112.6299,148.53093 168.82266,146.07576 88.611514,145.79838 34.510773,146.42924 ",
"fill": "#afafde"
},
"a2": {
"d": "m88.612 143.72 80.211-1.5957v3.9511l-80.211-0.27738z",
"points": "168.82266,142.1247 168.82266,146.07576 88.611514,145.79838 88.611514,143.72037 ",
"fill": "#e9e9ff"
}
}
}
]
I know that is something to do within the function but I am failing to set the code to do that. Thank you all who can suggest the correct code for it.