doukun1450 2018-02-09 10:15
浏览 75
已采纳

从JSON(PHP)中删除项目

I reviewed a lot of post to delete an item from JSON with PHP but somehow the 'unset' function is not working anywhere in my code. How can I delete 'SPEC_Info' from my JSON file with the code below I created to merge different JSON files (URLs)?

$sets = array();
foreach ($urls as $url)
{
    $json = file_get_contents($url);
    $data = json_decode($json);
    $json = substr($json, 1, -1);
    $sets = array_merge($sets, $data);
}

$new = json_encode($sets, JSON_PRETTY_PRINT);
echo '<pre>';
echo $new;

$fp = fopen('dsales_watchjewel_combined_en.json', 'w');
fwrite($fp, $new);
fclose($fp);
  • 写回答

3条回答 默认 最新

  • doutian3269 2018-02-09 10:19
    关注
    $sets = array();
    foreach ($urls as $url)
    {
        $json = file_get_contents($url);
        $decoded = json_decode($json, true);
        oreach ($decoded as $key => $innerObject) { //based on the fact that your JSON contains multiple inner objects.
            if(isset($innerObject['SPEC_Info'])) {
                unset($decoded[$key]['SPEC_Info']);
            }
        }
        $sets[] = $decoded;
    }
    $new = json_encode($sets, JSON_PRETTY_PRINT);
    echo '<pre>';
    echo $new;
    
    $fp = fopen('combined.json', 'w');
    fwrite($fp, $new);
    close($fp);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试