Rochak Kunwar
Rochak Kunwar

Reputation: 29

How to edit JSON with PHP?

<?php
global $obj;
global $books;
global $chapters;
global $verses;
global $newObj;

 $json = file_get_contents("json/jsoneng.json");
 $obj = json_decode($json, true);

 foreach($obj['books'] as $books){
     foreach ($books['chapters'] as $chapters){
         foreach($chapters['verses'] as $verses){

             echo $verses['text'] . "<br/>";
              $verses['text'] = "";      
            }
     }
 }


 $newObj = json_encode($obj);
 file_put_contents('json/what.json', $newObj);

?>

And this is how my JSON looks like:

{
    "books": [
        {
            "book": "1 Nephi",
            "chapters": [
                {
                    "chapter": 1,
                    "reference": "1 Nephi 1",
                    "verses": [
                        {
                            "reference": "1 Nephi 1:1",
                            "text": "I, Nephi, having been born of goodly parents, therefore I was taught somewhat in all the learning of my father; and having seen many afflictions in the course of my days, nevertheless, having been highly favored of the Lord in all my days; yea, having had a great knowledge of the goodness and the mysteries of God, therefore I make a record of my proceedings in my days.",
                            "verse": 1
                        },
                        {
                            "reference": "1 Nephi 1:2",
                            "text": "Yea, I make a record in the language of my father, which consists of the learning of the Jews and the language of the Egyptians.",
                            "verse": 2
                        },
                        {
                            "reference": "1 Nephi 1:3",
                            "text": "And I know that the record which I make is true; and I make it with mine own hand; and I make it according to my knowledge.",
                            "verse": 3
                        },

............................................

I want to delete the whole text and make it empty like "text": "",

But my code is not working it is saving the same original json file.

Upvotes: 1

Views: 64

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

You have to use passing by reference concept of php

Case 1: If the json structure will always same in each case, then

foreach($obj['books'][0]['chapters'][0]['verses'] as &$verses){//& => passing by reference concept
  $verses['text'] = '';
}

Output:- https://3v4l.org/sOEuV

Case 2: If json structure can change, then

foreach($obj['books'] as &$books){
     foreach ($books['chapters'] as &$chapters){
         foreach($chapters['verses'] as &$verses){ //& => passing by reference concept
            $verses['text'] = "";      
         }
     }
}

Output:- https://3v4l.org/1Uv5Y

Upvotes: 2

Rakesh Jakhar
Rakesh Jakhar

Reputation: 6388

You have to use the pass by reference with ampersand

foreach($chapters['verses'] as &$verses){
   $verses['text'] = "";      
}

Your code should be :-

foreach($obj['books'] as &$books){
   foreach ($books['chapters'] as &$chapters){
     foreach($chapters['verses'] as &$verses){
          $verses['text'] = "";      
        }
   }
 }

Working example

Upvotes: 3

Related Questions