deleting JSON array element in PHP, and re-encoding as JSON

前端 未结 3 1332
梦如初夏
梦如初夏 2021-01-15 13:10
function deleteNews($selected) {
    $file = file_get_contents(\'news.json\', true);

    $data=json_decode($file,true);
    unset($file);

foreach($selected as $ind         


        
3条回答
  •  执笔经年
    2021-01-15 13:19

    function deleteNews($selected) { $file = file_get_contents('news.json', true);

    $data=json_decode($file,true);
    unset($file);
    
    foreach($selected as $index){
      unset($data[$index]);
    }
    
    foreach($data as $value){
      $new_data[] = $value;
    }
    
    $result=json_encode($new_data);
    
    file_put_contents('news.json', $result);
    echo $result;
    unset($result);
    $url="./deleteNews.php";
    

    //redirect($url); }

提交回复
热议问题