Removing Specific Elements from Json Encoded Array by Use of PHP

Delete element from json with php

I have found the correct way to check and remove an element from JSON file.

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

How to unset value from json encode array using php

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

How to remove objects from JSON array

Since you have boolean true values in the array (that will match a type juggled true value such as string "Dispatched"), you need to pass true as the third parameter to in_array() for strict comparison.

Assuming you have run json_decode() and passed true for an array, just use strict comparison in in_array():

   if (in_array('Dispatched', $value, true)) {
unset($status[$key]);
}

In this case, knowing the key I personally would use:

   if ($value['value'] === 'Dispatched') {
unset($status[$key]);
}

Delete elements of a JSON encoded in a $_COOKIE

Last line should simply be:

setcookie('desire', json_encode($desire), time() + 604800);

EDIT: But this line

 $desire = json_decode(print_r($desire))

does not make much sense, it should not be there at all. print_r should be used to display values in a human-readable form and - without a second argument explicitly set to true - its return value does not make much sense.

EDIT #2: Complete cleaned up solution

<?php
$desire = json_decode($_COOKIE['desire']);

// Value of $desire before unset().
print_r($desire);
echo '<hr>';

// Unsetting element with index 0.
// Beware! 0 may not always be the first available index.
// If removing of the first element is desired, regardless its index, array_shift($desire) should be used instead.
unset($desire[0]);

// Value of $desire after unset().
print_r($desire);
echo '<hr>';

// Save the new $desire value in the cooke.
setcookie('desire', json_encode($desire), time() + 604800);
?>

Removing array index reference when using json_encode()

Use array_values() for your issue:

$arr['dates'] = array_values($arr['dates']);
//..
$arr = json_encode($arr);

Why? Because you're unsetting array's key without re-ordering it. So after this the only way to keep that in JSON will be encode keys too. After applying array_values(), however, you'll get ordered keys (starting from 0) which can be encoded properly without including keys.

deleting JSON array element in PHP, and re-encoding as JSON

Try it with array_values:

$result = json_encode(array_values($data));

How to remove first two JSON objects from JSON file using PHP

Try this:

<?php

$json = '[
{"name":"name1", "city":"city1", "country":"country1"},
{"name":"name2", "city":"city2", "country":"country2"},
{"name":"name3", "city":"city3", "country":"country3"},
{"name":"name4", "city":"city4", "country":"country4"},
{"name":"name5", "city":"city5", "country":"country5"}
]'; //file_get_contents('jason_file.json');

$json = json_encode(array_slice(json_decode($json, true), 2));
/* (1) decode the JSON string
<-----------
(2) cut off the first two elements
<-----------
(3) recode as JSON
*/

echo $json;

//file_put_contents('jason_file.json, $json);

Output:

[{"name":"name3","city":"city3","country":"country3"},{"name":"name4","city":"city4","country":"country4"},{"name":"name5","city":"city5","country":"country5"}]

How to delete JSON keys from an array with PHP?

// make array
$array = json_decode($your_json_string, true);

// loop through array
foreach($array as $key => $item){
// unset them
unset($array[$key]["phonenumber"]);
}

// make json again
$json_string_modified = json_encode($array);

OR using reference

// make array
$array = json_decode($your_json_string, true);

// loop through array using reference
foreach($array as &$item){

// unset specific key
unset($item["phonenumber"]);

}

// unset reference
unset($item);

// make json again
// you may remove JSON_PRETTY_PRINT flag, I kept it just to see o/p
$json_string_modified = json_encode($array,JSON_PRETTY_PRINT);


Related Topics



Leave a reply



Submit