2013-08-13 62 views
6

Tôi xin lỗi vì tiếng anh của tôi. Tôi biết rằng có rất nhiều câu hỏi khác như thế này nhưng tôi đã không tìm thấy bất cứ điều gì có thể giúp tôi (hoặc có lẽ tôi không hiểu).Xóa phần tử từ json bằng php

Tôi có một json như thế này (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" 
} 
] 

Tôi muốn xóa một phần tử (một chiếc xe, ví dụ Renault Megane) từ json với php. tôi viết một hàm như thế này:

$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 

mà mã để tôi viết cho loại bỏ bất cứ tài sản (đối với xe mà chúng tôi muốn loại bỏ, ví dụ Renault Megane) từ tập tin json?

Trả lời

17

Sau một ngày làm việc tôi đã tìm được cách chính xác để kiểm tra và loại bỏ một phần tử từ tập tin 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++; 
} 

Tôi không biết lý do nhưng tôi đã trả lời câu hỏi một vài ngày trước nhưng đã bị xóa. tôi hy vọng câu trả lời này sẽ được kiểm tra chính xác. Tôi xin lỗi vì trình độ học tiếng Anh của tôi.

+1

Vấn đề với giải pháp này là nếu chúng tôi đang làm việc với một loạt các đối tượng sau này json_encode nó trở thành một đối tượng với một số đối tượng bên trong và thuộc tính của đối tượng chính là khóa của vị trí mà nó đã từng có trên mảng. Để khắc phục điều này sau khi foreach chúng ta nên làm một $ data = array_values ​​($ data) để rebase mảng chính. – Murilo

+0

@Murilo Cảm ơn bạn đã chỉ ra điều đó, và điều này nên là một phần của câu trả lời, nhưng tôi không chắc tại sao tác giả lại không thấy vấn đề với cách tiếp cận này. Ngoài ra trả lời trong câu hỏi này đáng kể hơn nữa xuống trong kết quả tìm kiếm của tôi: https://stackoverflow.com/a/26316131/1827734 Với một lời giải thích sâu tại https://stackoverflow.com/a/27088740/1827734 – BrDaHa

0
foreach ($data as $key => $element) { 
    $value = $element['...']; 
    if (...) { 
     unset($data[$key]); 
     // or 
     $data[$key]['...'] = '...'; 
    } 
} 
0
<?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); 
?> 

câu hỏi tương tự

JSON Search and remove in php?

Delete from json using php

+0

Tôi xin lỗi, nhưng giải pháp của bạn không hoạt động (nếu tôi đổi $ động vật thành $ ô tô);) –

0

Dễ dàng hơn theo cách đó.

//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)); 

Điều này sẽ làm tốt. Tin tôi đi!

Các vấn đề liên quan