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.