-1

How can i delete value from json encode array using php. here is my json format.

{"1":["fileid",time],"2":["fileid",time],"3":["fileid",time]}

And here is my php code. but nothing is delete. i want to remove whole array value ex

"3":["fileid",time]

when function execute.

<?php
$filename = "test.json";
$fopen = fopen($filename,"r");
$fgets = fgets($fopen);
fclose($fopen);
$decode = json_decode($fgets,true);
foreach($decode as $post){
    $dif = time() - $post[1];
    if($dif > 5){
        foreach ((array)$post[0] as $val => $v) {
            echo  $id = $v.'<br>';
        }
        unset($decode[1]);   
    }
?>

Thanks

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

1 Answers1

1

You need an index, rather than always unsetting occurance 1. So change your foreach as below and use that index $i on the original array

You can also simplify the file read to file_get_contents()


$buf = file_get_contents("test.json");
$array = json_decode($buf,true);

foreach($array as $i => $post){
    $dif = time() - $post[1];
    if($dif > 5){
        unset($array[$i]);  
    }
}
file_put_contents("test2.json",json_encode($array)); 
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Thanks for your help, can you modify it little i have more values inside that json in concatenate or string, i want to get separate value to call them inside function as single id. – Dinesh chandra Oct 14 '21 at 15:26
  • Don't toally understand, can you show a sample of the data and what you want to do with it – RiggsFolly Oct 14 '21 at 15:27
  • ```{"1":["fileid",time],"2":["fileid",time],"3":["fileid",time]}``` suppose these are the values, but it returns all ```$post[1]``` values as a single string but i have pass single fileid as a string in my function. All File ids are joined as a single string. – Dinesh chandra Oct 14 '21 at 15:29
  • Still not sure I understand. I see no function, so maybe if you ADD this next part to your question with an example of what you mean – RiggsFolly Oct 14 '21 at 15:35
  • I'm getting data in this format ```fileidtimefileidtimefileidtime``` it is return value like this i want to store single value in var instead of whole data, so how to seprate values using foreach loop after ```if($dif > 5){``` this. i need only fileid or ```$post[0]``` value. Hope you understand what i'm trying to say. – Dinesh chandra Oct 14 '21 at 15:55
  • Well you see I dont see you doing anything like that in the code you show, if you EDIT your question and ADD this code you are talking about I will be able to work out what you want to do I am pretty sure – RiggsFolly Oct 14 '21 at 16:07
  • I updated my question. – Dinesh chandra Oct 14 '21 at 16:17
  • Thanks for your precious time mate, i build that what i need. – Dinesh chandra Oct 14 '21 at 16:20
  • So you only want to echo some data, IF the $dif is greater than 5. – RiggsFolly Oct 14 '21 at 16:20
  • But what do you actually want to print? – RiggsFolly Oct 14 '21 at 16:21
  • I want to delete data after sometime, but now i fixed and its working. – Dinesh chandra Oct 14 '21 at 16:23