0

Hi there im trying to update a nested array I have done some research and have come up with the following question but the answer isnt quite clear on how to update where x = this.

LINK How to updated nested array

$yourArray["experience"][1]["from"] = 2006;

That line looks like what I need but I need to figure out how to use the update part of it, this is my post script for my form.

$ID = $_POST["id"];
$NAMESTATE = $_POST["name"];
$REASON = $_POST["reason"];
require ('mongo.php');


if ($WISHNAMESTATE == "" and $ID == "")
{
    echo("OOPS SOMETHING WENT WRONG!");
    exit();
}
else
{
$m->update(array('_id' => $ID),(array('fields.NameState._0' => $NAMESTATE)));
   header("refresh:0;url=names_test.php"); 

My db looks like this.

array(
"_id"=>100000005,
"dclass"=>"Distributed",
    "fields"=>array(
"Name"=>array(
    "_0"=>"Testing",
),
"NameState"=>array(
    "_0"=>"PENDING",
),
 'setName': {
    '_0': 'test name'

So what this post needs to do is take the value NAMESTATE and update that field in the document with the matching ID, Please let me know if I am on the right track here.

Community
  • 1
  • 1
Ryan
  • 61
  • 1
  • 10
  • I have done some more research and using this page http://stackoverflow.com/questions/10522347/mongodb-update-an-object-in-nested-array?rq=1 i have come up with this `$m->update({'_id' : $ID , 'fields.NameState._0' : $NAMESTATE});` but Dreamweaver is giving me a syntax error – Ryan Nov 10 '14 at 13:59

1 Answers1

1

$_POST contains just String variables. If documents' _id field is Integer, you must be cast to int to $ID;

$m->update(array('_id' => (int) $ID),(array('fields.NameState._0' => $NAMESTATE)));
alu
  • 456
  • 2
  • 7