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 8858685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:53:04+00:00 2026-06-14T14:53:04+00:00

I’m trying to search for an id in my json file and and remove

  • 0

I’m trying to search for an id in my json file and and remove that object with php.

In detail: I post the id with jQuery post to my delete.php and in my delete.php file I search for the id and unset it. But for some reason if I unset the first object it adds a number to my json and breaks it (I can still add more objects but breaks the first one).

// delete.php file looks like this:

if(isset($_POST['deleteData'])){
    $data = $_POST['deleteData'];
    $dataJSON = json_decode(file_get_contents('datas.json'), true);  
    for($i = 0, $dataJSONLength = count($dataJSON); $i < $dataJSONLength; $i++){
      if($dataJSON[$i][data][0][id] == $data){
        unset($dataJSON[$i]);
        echo 'deleted';// needed for callback for feedback
      }
    }
    file_put_contents('datas.json', json_encode($dataJSON));
  }

 
 

// add.php is:

if(isset($_POST['addData'])){
  $dataJSON = json_decode(file_get_contents('datas.json'), true);
  $dataJSON[] = $_POST['addData'];
  file_put_contents('datas.json', json_encode($dataJSON));
}

My datas.json file is fairly nested so I’ll just post where the issue is:

[{"user":[{"browser":[{" // this is what it looks like when I add

{"1":{"user":[{"browser":[{" // this is what happens when I delete the first object. Notice "1" 

If I don’t delete the first object everything works fine until the first object is deleted. Any suggestions? Thanks. (if needed I can post the whole json file)

  • 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-06-14T14:53:05+00:00Added an answer on June 14, 2026 at 2:53 pm

    PHP is turning your array into an object probably because unset is giving you an associative array rather than in indexed array. You should call array_splice instead of unset or else call array_values after unsetting your value per the ideas found in this post.

    // your for loop:
    for (...) {
        unset($dataJSON[$i]);
        ...
    }
    // After your for loop with all necessary values removed then call array_values to normalize the array.
    $dataJSON = array_values($dataJSON);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.