0

I have a column with json structure:

[{"id":3},{"id":4,"children":[{"id":5,"children":[{"id":6}]}]}]

The value of id that comes with request is 4.

How can i delete id of value 4 and his children ?

I except output like this:

[{"id":3}]
i1919
  • 53
  • 6

1 Answers1

0

You can use the second parameter of json_decode to get a php array and work with php built-in functions.

$string = '[{"id":3},{"id":4,"children":[{"id":5,"children":[{"id":6}]}]}]';
$result = array_filter(json_decode($string, true), function($elem){return $elem['id']!=4;});

array_filter keeps only the elements for which the callback function returns true, so this keeps all elements where id is not 4

blues
  • 4,547
  • 3
  • 23
  • 39