【问题标题】:Delete element from json with php用php从json中删除元素
【发布时间】:2013-08-13 15:20:35
【问题描述】:

对不起我的英语。 我知道还有很多其他类似的问题,但我没有找到任何可以帮助我的东西(或者我不明白)。

我有一个这样的 json (autocaricate.json):

[
{
"nome":"LABORGHINI GALLARDO",
"descrizione":"LAMBORGHINI GALLARDO ED. NERA- ANNO 2007- ",
"indirizzo_pubblicato":"autocaricateeea\/LABORGHINI GALLARDO31072013-023853\/LABORGHINI GALLARDO31072013-023853.json",
"indirizzo_immagine_copertina":"autocaricateeea\/LABORGHINI GALLARDO31072013-023853\/IMG_1414 (600x448).jpg",
"indirizzo_paginaauto":"autocaricateeea\/LABORGHINI GALLARDO31072013-023853\/index.html"
},
{
"nome":"RENAULT MEGANE",
"descrizione":"RENAULT MEGANE -ANNO 2006-DIESEL-CC. 1461",
"indirizzo_pubblicato":"autocaricateeea\/RENAULT MEGANE31072013-024103\/RENAULT MEGANE31072013-024103.json",
"indirizzo_immagine_copertina":"autocaricateeea\/RENAULT MEGANE31072013-024103\/P1080949 (600x450).jpg",
"indirizzo_paginaauto":"autocaricateeea\/RENAULT MEGANE31072013-024103\/index.html"
},
{
"nome":"FORD MONDEO",
"descrizione":"FORD MONDEO SINISTRATA- ANNO 2009- DIESEL- CC. 1997-",
"indirizzo_pubblicato":"autocaricateeea\/FORD MONDEO31072013-045216\/FORD MONDEO31072013-045216.json",
"indirizzo_immagine_copertina":"autocaricateeea\/FORD MONDEO31072013-045216\/P1080971 (600x450).jpg",
"indirizzo_paginaauto":"autocaricateeea\/FORD MONDEO31072013-045216\/index.html"
}
]

我想用 php 从 json 中删除一个元素(汽车,例如 RENAULT MEGANE)。 我写了一个这样的函数:

$url = $GET['indirizzo']; //this variable get an address like autocaricateeea\/RENAULT MEGANE31072013-024103\/index.html

$file = file_get_contents('autocaricate.json');
$data = json_decode($file);
unset($file);//prevent memory leaks for large json.
//insert data here
foreach($data as $elemento) {
    $valore = $elemento['indirizzo_paginaauto'];
    if($valore == $url){
        ****** enter code here ******
    }
}
//save the file
file_put_contents('autocaricate.json',json_encode($data));
unset($data);//release memory

我为从 json 文件中删除任何属性(对于我们要删除的汽车,例如 RENAULT MEGANE)编写哪些代码?

【问题讨论】:

    标签: php json


    【解决方案1】:

    我找到了从 JSON 文件中检查和删除元素的正确方法。

    $i=0;
    foreach($data as $element) {
       //check the property of every element
       if($url == $element->indirizzo_paginaauto){
          unset($data[$i]);
          echo ("elemento cancellato");
       }
       $i++;
    }
    

    【讨论】:

    • 这个解决方案的问题是,如果我们在这之后使用一个对象数组,如果我们尝试一个 json_encode,它就会变成一个里面有几个对象的对象,主对象的属性是关键它曾经在阵列上的位置。为了在 foreach 之后解决这个问题,我们应该做一个 $data = array_values($data) 来重新定位主数组。
    • @Murilo 感谢您指出这一点,这应该是答案的一部分,但我不确定为什么作者没有看到这种方法的问题。在我的搜索结果中也进一步回答了这个问题:stackoverflow.com/a/26316131/1827734stackoverflow.com/a/27088740/1827734有深入的解释
    • 除了@Murilo 解决方案,他还需要在 IF 条件下,在 unset() 之后递减 $i ($i--)。
    【解决方案2】:

    这样做更容易。

    //get all your data on file
    $data = file_get_contents('teste_data.json');
    
    // decode json to associative array
    $json_arr = json_decode($data, true);
    
    // get array index to delete
    $arr_index = array();
    foreach ($json_arr as $key => $value) {
        if ($value['YOUR KEY'] == SOME VALUE TO COMPARE) {
            $arr_index[] = $key;
        }
    }
    
    // delete data
    foreach ($arr_index as $i) {
        unset($json_arr[$i]);
    }
    
    // rebase array
    $json_arr = array_values($json_arr);
    
    // encode array to json and save to file
    file_put_contents('teste_data.json', json_encode($json_arr));
    

    这样就可以了。相信我!

    【讨论】:

      【解决方案3】:
      <?php
      
      $cars = json_decode($cars , true); // $cars is the json array before decoding
      foreach ($cars as $key => $value) {
          if (in_array('RENAULT MEGANE', $value)) {
              unset($cars[$key]);
          }
      }
      $cars = json_encode($cars );
      ?>
      

      类似的问题

      JSON Search and remove in php?

      Delete from json using php

      【讨论】:

      • 很抱歉,您的解决方案不起作用(如果我将 $animals 更改为 $cars);)
      【解决方案4】:
      foreach ($data as $key => $element) {
          $value = $element['...'];
          if (...) {
              unset($data[$key]);
              // or
              $data[$key]['...'] = '...';
          }
      }
      

      【讨论】:

        【解决方案5】:
        getpath = file_get_contents('file.json');
        $arr = json_decode($getpath, true);
        for ($i = 0; $i < count($arr); $i++) {
            if($arr[$i]['id']==$id){
                unset($arr[$i]);
                $arr = array_values($arr);
        
                // encode array to json and save to file
                file_put_contents('file.json', json_encode($arr));
         
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-18
          • 2023-03-22
          相关资源
          最近更新 更多