2016-09-24 16 views
5

Mam plik json patrząc tak:Usuwanie rekordu JSON z .json pliku PHP

[["{\"id\":1474721566304,\"name\":\"GGG\",\"brand\":\"GG\",\"price\":\"3\"}"],["{\"id\":1474721570904,\"name\":\"GGGH\",\"brand\":\"GGH\",\"price\":\"4\"}"],["{\"id\":1474721574188,\"name\":\"GGGH\",\"brand\":\"GGHH\",\"price\":\"5\"}"]] 

Co staram się zrobić, to usunąć rekord z nim przez to id. W tym celu mam następujący kod PHP:

<?php 
$string = file_get_contents("products.json"); 
$json_a = json_decode($string, true); //turning JSON-string into an array containing JSON-strings 

$arr = array(); 
foreach ($json_a as $key) { 
    array_push($arr,json_decode($key[0],true)); //and here you turning each of the JSON-strings into objects themselves 


} 
$data= $_GET['data']; 
$i=0; 
foreach($arr as $element) { 
    if($data == $element["id"]){ 
     unset($arr[$i]);//removing the product by ID 
    } 
    $i++; 
} 

var_dump($arr); 
$arr2 = array(); 

foreach ($arr as $key) {//trying to make it look like the original json. 
    array_push($arr2,json_decode($key[0],true)); 
} 
//var_dump($arr2); 
echo json_encode($arr2); 
?> 

Co ja dostaję z tego kodu jest:

array(2) { [0]=> array(4) { ["id"]=> float(1474721566304) ["name"]=> string(3) "GGG" ["brand"]=> string(2) "GG" ["price"]=> string(1) "3" } [1]=> array(4) { ["id"]=> float(1474721570904) ["name"]=> string(4) "GGGH" ["brand"]=> string(3) "GGH" ["price"]=> string(1) "4" } } 

Jestem naprawdę z pomysłów, jak zrobić to tablica wyglądać mojego pierwotnego JSON pokazanej pierwszy w tym poście. Próbowałem wielu różnych rzeczy, ale nie mogłem tego zrobić. Mój pomysł polega na usunięciu rekordu przez jego identyfikator, aby zastąpić stary JSON nowym, który próbuję tutaj skonstruować.

Jestem nowy w php i byłbym wdzięczny za wszelkie uwagi dotyczące mojego problemu.

+0

można uzyskać klucz tablica w następującej pętli: 'foreach ($ arr jako $ key => $ value) {if (dane == $ wartość ['id]) {unset $ arr [$ key];}}' – JazZ

+0

Hej, @ AdrienLeber. To nie jest mój problem i już mam to, co zasugerowałeś w powyższym kodzie. Mój problem polega na konstruowaniu nowego JSON po tym kroku. –

+0

Rozumiem, dlatego zamieszczam to jako komentarz. ;) Juste, ponieważ iterujesz z $ i, ale nie potrzebujesz tego z 'foreach'. To była tylko wskazówka. Co się stanie, jeśli wyślesz nową tablicę do pliku? – JazZ

Odpowiedz

1

Przede wszystkim - wprowadzenie danych JSON jest źle:

array (size=3) 
    0 => 
    array (size=1) 
     0 => string '{"id":1474721566304,"name":"GGG","brand":"GG","price":"3"}' (length=58) 
    1 => 
    array (size=1) 
     0 => string '{"id":1474721570904,"name":"GGGH","brand":"GGH","price":"4"}' (length=60) 
    2 => 
    array (size=1) 
     0 => string '{"id":1474721574188,"name":"GGGH","brand":"GGHH","price":"5"}' (length=61) 

Nie masz 'id' ani 'name', 'GGG' oraz innych klawiszy. Masz tylko jeden długi ciąg. Powinieneś usunąć niepotrzebne znaki cudzysłowu. Po że JSON powinna wyglądać następująco:

[[{"id":1474721566304,"name":"GGG","brand":"GG","price":"3"}],[{"id":1474721570904,"name":"GGGH","brand":"GGH","price":"4"}],[{"id":1474721574188,"name":"GGGH","brand":"GGHH","price":"5"}]] 

i wreszcie, Twój kod PHP może być znacznie krótsza:

$json = "[[{\"id\":1474721566304,\"name\":\"GGG\",\"brand\":\"GG\",\"price\":\"3\"}],[{\"id\":1474721570904,\"name\":\"GGGH\",\"brand\":\"GGH\",\"price\":\"4\"}],[{\"id\":1474721574188,\"name\":\"GGGH\",\"brand\":\"GGHH\",\"price\":\"5\"}]]"; 
$input = json_decode($json, true); 
$output = array(); 
foreach($input as $element) { //you don't need to declare yet another array, just use the one you already have 
    if($_GET['data'] != $element[0]["id"]){ //and not unset, just add to new array if you want 
     $output[] = $element; //shorter and faster than array_push() 
    } 
} 
echo json_encode($output); 
+0

Dzięki, dobrze wiedzieć :) Chciałbym również wiedzieć, dlaczego moja odpowiedź została odrzucona, więc mógłbym ją edytować. – ElChupacabra

0

Spróbuj kod:

$string = file_get_contents("product.json"); 
$json_a = json_decode($string, true); //turning JSON-string into an array containing JSON-strings 

$arr = array(); 
foreach ($json_a as $key) { 
    array_push($arr,json_decode($key[0],true)); //and here you turning each of the JSON-strings into objects themselves 
} 
$data= 0; 
$i=0; 
foreach($arr as $element) { 
    if($data == $element["id"]){ 
     unset($arr[$i]);//removing the product by ID 
    } 
    $i++; 
} 

// print_r($arr); 
$arr2 = array(); 
foreach($arr as $key => $val) { 
    $arr2[][] = $val; 
} 
//var_dump($arr2); 
echo json_encode($arr2); 
+0

Dzięki za Twój kod. Udało mu się kilka modyfikacji. –