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

  • Home
  • SEARCH
  • 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 9292359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:51:36+00:00 2026-06-18T20:51:36+00:00

Background: I have a class in this application I’m building whose job is: __construct:

  • 0

Background:

I have a class in this application I’m building whose job is:

  • __construct: $this->data = $this->mongoDB->collection->findOne();
  • Intermediate functions are employed to manipulate the data in tens of different ways each request. One manipulation could trigger one which would trigger another. This allows me to do unlimited updates to the mongo document with just one query, as long as $this->data['_id'] remains the same. This is the only place where data manipulation of this specific collection is allowed.
  • __destruct: $this->monboDB->collection->save($data)
  • Data is then read back, json_encode‘d and sent to Javascript to draw the page

Intention:

I intended to delete a member of an array by looping through said array, matching a value within it, and unsetting that. Example:

foreach($this->data['documents'] as $key => $val){
    if($val == $toBeDeleted){
        unset($this->data['documents'][$key];
    }
}

Then, this would be saved to the DB when the script finishes.

Problem:

When javascript reads back the data, rather than having ['a', 'b', 'd'], I had {'0': 'a', '1': 'b', '3': 'd'} – which can’t be treated like an array and would pretty much break things.

  • 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-18T20:51:38+00:00Added an answer on June 18, 2026 at 8:51 pm

    I had this question half typed out before my a-hah! moment, so I figured I’d post my own answer to it too for future reference.

    In php, an associative array and an array are all the same. You can have out of order keys, nonconsecutive keys, and almost any key that you’d like to use in calling your array member. Most, if not all, php array functions work with any array key. Objects are a totally different thing.

    That being said, Javascript doesn’t share the same rules for arrays. A javascript array must have consecutive keys starting at zero, otherwise it is an object. MongoDB is similar to Javascript in this way.

    When php converts an object to be used in MongoDB or in Javascript, if the php array doesn’t follow that rule, it becomes a Javascript object.

    The problem was after unsetting an array index, it left a gap, causing nonconsecutive array keys, causing it to become an object. Simple fix would either be array_slice($array, $key, 1) or $array = array_values($array)

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

Sidebar

Related Questions

I have a background worker which calls a function within a separate class. This
I have this CSS. the property FILTER In class .crumb make my background and
I have a class to launch background operations in a WinForms application. I need
I have a class Communicator that works in a background thread receiving data on
Say I have a class named testThing: .testThing { background-color:#000000; float:left; height:50px; width:50px; }
I have an IValueConverter class that is used to change the background color of
I have a helper class that should only ever run in a background thread.
I basically have the following class: .sf-sub-indicator { background: url(/abcprod/images/arrows-ffffff.png) no-repeat -10px -100px; }
lets say i have a background worker in a class that perform db query
I have created a button CSS class that uses background images with different positions

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.