doumiang0597 2018-08-14 03:54
浏览 83
已采纳

PHP - 将JSON附加到新类别中的现有文件

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.

  • 写回答

3条回答 默认 最新

  • dongtong7990 2018-08-14 04:50
    关注

    I'm not sure if I completely understand your requirements (and I am coding from my phone), but I recommend generating the complete file contents before writing to the file (only once).

    Do away with unnecessary levels and prefixed counters...

    $tempArray = [];
    foreach ($split as $item) {
        $attrib_arr = parseTag($item, 'path');
        if (!empty($attrib_arr)) {
            $tempArray[] = $attrib_arr;
        }
    }
    if (!empty($tempArray)) {
        $data_array = json_decode(file_get_contents('newfile.json'), true);
        $data_array["x"][] = $tempArray;
        $jsonData = json_encode($data_array, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        file_put_contents('newfile.json', $jsonData);
    }
    

    In this snippet, the foreach loop creates the "current batch" of data. If it is not empty, it will store it in its own group under the x parent key. This way you can always differentiate the data from one batch to another.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法