简体   繁体   中英

how to add an array in nested array (PHP Mongodb)

I have created a data set below:

$dataset = array("name" => "Cat", 
    "uid" => 20, 
    "posterPath" => "http://xyz.png", 
    "rank" => 1, 
    "status" => 1, 
    $dataset["sources"] = array(array("url" => "xyz://1/0", 
            "sId" => "10", 
            "type" => "89"));

I have stored this data set in MongoDB now, I want to add one more source in sources array. I have tried with unable to do so. First, I loaded the dataset from MongoDB then I loaded sources manually $dataset["0"]["sources"]; , but now how to add one source and add that to the database.

Please help.

If your array structure is this:

$dataset = array("name" => "Cat", 
"uid" => 20, 
"posterPath" => "http://xyz.png", 
"rank" => 1, 
"status" => 1, 
"sources" => array(
                array("url" => "xyz://1/0", 
                        "sId" => "10", 
                        "type" => "89")
            )
);

You can add array to sources with:

$dataset['sources'][]=  array("url" => "xyz2://1/0", 
                        "sId" => "11", 
                        "type" => "90");
$arrSource[] = [
             "url" => "xyz://1/0",
             "sId" => "10",
             "type" => "89"
        ];

//...Here you can push data in $ arrSource

$dataset = [
           "name" => "Cat",
           "uid" => 20,
           "posterPath" => "http://xyz.png",
           "rank" => 1,
           "status" => 1,
           "sources" => $arrSource
        ];

Now, if you have wished to add one source to sources then just push data in the $arrSource array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM