Home:ALL Converter>How to apply a filter to update element in array of arrays?

How to apply a filter to update element in array of arrays?

Ask Time:2018-10-10T17:36:08         Author:Ciconia Grullera

Json Formatter

I am trying to create a PHP application that lets me uploading data to a MongoDB collection. For that I have "installed" the PHP driver with no problems.

However, I cannot find anyway -neither in the PHP guide- how could I update an element in an array of arrays.

Collection structure

As you may see, _unidades is an array of arrays. Each of those arrays contains an ID, a string and another array. In my case, which will be chosen depends on previous param -it has to match with element 1 of one of them.

Once I have selected that structure, I want to insert a new array into its array of arrays (position 2).

Regarding code, I tried the following:

$bulk = new MongoDB\Driver\BulkWrite();
    $bulk->update(
               [
                   '_isbn' => (int) $_POST["isbn"],
                   '_topics' =>
                        [
                            '0' => (int) $_POST["topic"]
                        ]
               ],
               [
                   '$set' => [
                            '1' => array($_POST["sentence"], $_POST["question"])
                       ]
               ]
            );        

    $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 100);
    $resultado = $manager->executeBulkWrite('libreee.faq', $bulk, $writeConcern);

However as you can see I am not capable to determine at least that it doesn't have to be an specific array (7th line).

Once said that I look forward to receiving your help. Thanks a lot in advance.

Regards, Ciconia.

Author:Ciconia Grullera,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/52737084/how-to-apply-a-filter-to-update-element-in-array-of-arrays
yy