Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6910145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:46:36+00:00 2026-05-27T08:46:36+00:00

So currently I’m working on a project that requires the use of PHP and

  • 0

So currently I’m working on a project that requires the use of PHP and JSON. The way I have currently set up my code is so that I read the JSON file in using file_get_contents and then use the built in json_decode() function. Everything is reading perfectly, it’s the writing part that is giving me trouble.

The way I have my JSON tree set up is:

{
"test":[
        {
        "testName":"Winter 2011",
        "testDate":"12/04/2011",
        "testTime":"2:00pm",
        "testRoom":"room",
        "cap": 10,
        "spotsLeft": 0,
        "student":[
            {
                "fname":"Bob",
                "lname":"Roberts",
                "usrnm":"bobr",
                "email":"bob@gmail.com"
            }
        ]
    }
]
}

To get the right test (since there will be multiple tests) I’m using a foreach loop and then I make sure there isn’t a duplicate. At this point I’m ready to add a new student to the JSON file so I call:
`

$file = file_get_contents('testDates.json');
$json = json_decode($file, true);

//Go through each test in the file
foreach ($json[test] as $t){
   .....
   //Works if I only do $t[student] but that only adds it to the $t[] and not the
   //JSON file. Can't seem to get it to add to the whole JSON string.
   array_push($json[$t][student], $newStudent);
   echo json_encode($json);
   .....
   //Use a file_put_contents later to write the file out

Any thoughts would be extremely helpful. It’s probably a pointer error to a certain point int he array but a second set of eyes never hurt. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:46:37+00:00Added an answer on May 27, 2026 at 8:46 am

    $t is a copy, it is not referencing original object. This appens with every foreach cycle. Use instead:

    foreach ($json[test] as $k => $t){
        array_push($json[test][$k][student], $newStudent);
        ...
    }
    

    Or you can try:

    foreach ($json[test] as &$t){
        array_push($t[student], $newStudent);
        ...
    }
    

    See here to learn how foreach works: http://php.net/manual/en/control-structures.foreach.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I'm working on a project where I have a server - client relationship
Currently I have this code: <?php echo '<meta name=robots content=noindex>'; $arr = json_decode(file_get_contents(http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json),true);
Currently I have 2 ways of displaying images in a cell, which way will
Currently working with converting SQLException error messages into messages that are more useful for
Currently, I have a log file of messages in one table in a MySQL
Currently I have in my code tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addCompoundToLabel:)]; My code
I want use html5's new tag to play a wav file (currently only supported
Currently I have a UITableview, I have enough data that cannot be shown entirely
Currently I have alot of information that are in several different divs. I want
Currently working on database part of android project. The main aim of the project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.