4

I have a json file looking like this :

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

What I am trying to do is to remove a record from it by it's id. For this purpose I have the following PHP code :

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

What I am getting from this code is :

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" } }

I am really out of ideas how to make this array look like my original JSON shown first on this post. I tried many different things, but I couldn't make it work. My idea is after removing the record by it's ID to replace the old JSON with the new one that I am trying to construct here.

I am new to php and I'd appreciate any input on my issue.

4
  • You can get the key of the array in the loop like this : foreach ($arr as $key => $value) {if (data == $value['id]) {unset $arr[$key];}} Commented Sep 24, 2016 at 19:15
  • Hey, @AdrienLeber. This isn't my issue and I've already have what you suggested in the code above. My issue is constructing the new JSON after this step. Commented Sep 24, 2016 at 19:17
  • I see, that's why I post it as a comment. ; ) Juste because you iterate with $i but you don't need this with foreach. It was just a tip. What happens if you send the new array in the file ? Commented Sep 24, 2016 at 19:22
  • A-haa, thanks for the tip. Regarding your question - i haven't tried that, but why would I? Commented Sep 24, 2016 at 19:25

2 Answers 2

1

First of all - your input JSON is wrong:

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)

You don't have 'id' nor 'name', 'GGG' and other keys. You just have one long string. You should remove unnecessary quotation marks. After that your JSON should look like this:

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

And finally, your PHP code can be much shorter:

$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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, good to know :) I'd also like to know why did my answer get downvoted so I could perhaps edit it.
0

Try this code:

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

1 Comment

Thanks for your code. It worked it some modifications.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.