susanBuck / e15-spring22

0 stars 0 forks source link

adding to a json file using php #37

Closed gabichuela85 closed 2 years ago

gabichuela85 commented 2 years ago

I know we're going to be working with keeping data in a database as opposed to a json file soon, but I wanted to know if it was possible to add a new $key=>$value pair to an entry in a json file.

For instance, with the books.json file, would it be possible to add an 'id' key to each entry? So that once the create() function was built out it would automatically add an 'id' => $i to the entry?

I've been trying to do this using a foreach loop, but every time the new pair is added to the entry, the $slug is omitted from the array.

susanBuck commented 2 years ago

Hi @gabichuela85 -

Yup, that's possible.

We've seen how we can read in JSON data and convert it to an array. If we wanted to make edits, we could just amend that array, then convert it back to JSON and write it back to the JSON file.

Can you share the code you've tried that is not producing the results you're expecting?

gabichuela85 commented 2 years ago

I am attempting to add id numbers to a movie.json file that I created after scraping a friend's movie collection for data.

I am using the following code to loop through each entry to add an id number:

$i = 1; 
foreach ($movies as $movie) {
$movie = Arr::add($movie, 'id', $i); 
$i++; 
var_dump($movie); 
}

var_dump($movies); 

I included the var_dump() to see ouput. So the output of var_dump($movie) looks like this:

Screen Shot 2022-03-16 at 12 41 35 PM

And the output of var_dump($movies) looks like this:

Screen Shot 2022-03-16 at 12 40 30 PM

So while the first var_dump() has the id affixed to the entry, the second var_dump() has the $slug but no 'id' added to the array.

susanBuck commented 2 years ago

FYI Taking a closer look at this now and will reply shortly.

susanBuck commented 2 years ago

I played around with your form method in MovieController.php and got it to work with these edits:

public function form()
{
    $movieList = file_get_contents(database_path('movies.json'));
    $movies = json_decode($movieList, true);

    $i = 1;
    foreach ($movies as &$movie) {
        $movie = Arr::add($movie, 'id', $i);
        $i++;
    }

    $movies = json_encode($movie);

    return view('movies/review');
}

Edit 1) Move the line $movies = json_encode($movie); outside of the for loop.

Edit 2) Add an ampersand before the $movie in the foreach definition (foreach ($movies as &$movie)). This will pass the $movie variable by reference which means any edits we make to it in the for loop will persist.

For more on this latter "by reference" technique check out the explanation from the php.net foreach docs: https://www.php.net/manual/en/control-structures.foreach.php

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

Let me know if you have any questions on that, as it's not something we've explicitly covered.

Also, let me know if the above code isn't doing what you're expecting.

gabichuela85 commented 2 years ago

This was very helpful. Thank you!

susanBuck commented 2 years ago

Closing this thread but feel free to re-open or start a new thread if there are any new questions. : )