简体   繁体   中英

Add element to PHP associative array in loop

I've looked but can't find an answer for my specific use case. I want to add an element to a multidimensional array while looping through it. What I have before the loop:

Array
(
    [fname] => Monty
    [lname] => Python
    [phone] => 555 555 1212
    [email] => a@b.com
    [modelList] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [modelName] => X-Wing
                    [remarks] => 
                    [htmlRemarks] => 
                    [category] => Vehicles
                    [catID] => 178
                    [attachedToBase] => 1
                    [oversized] => 0
                )

        )

)

In code, I'm looping through the [modelList] array and after doing some database operations what I want to do is append new elements to each model array - in the case below, the [dbID]:

Array
(
    [fname] => Monty
    [lname] => Python
    [phone] => 555 555 1212
    [email] => a@b.com
    [modelList] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [modelName] => X-Wing
                    [remarks] => 
                    [htmlRemarks] => 
                    [category] => Vehicles
                    [catID] => 178
                    [attachedToBase] => 1
                    [oversized] => 0
                    [dbID] => 907
                )

        )

)

All inputs are from a form POST, and in my php handler:

// Loop thru model entries
$modelList = json_decode($_POST["modelList"], TRUE); 
foreach($modelList as $model) {

    (do some work)

    // Add the new element
    array_push($model['dbID'], $newID);
}

But this throws an error:

PHP Warning:  array_push() expects parameter 1 to be array, null given

How can I add the new element to the sub-array?

array_push

Push one or more elements onto the end of array

You can't add a key value item to an array using array_push .
Use this instead:

foreach ($modelList as $key => $model){
  $modelList[$key]['dbID'] = $newID;
}

Based on your snippet with the loop are you trying to actually update the dbID ?

If so, you should change the logic to:

foreach ($modelList as $key => $model) {

    (do some work)
   
    $modelList[$key]['dbID'] = $newID;
}

Why:

  • you cannot change contents of the array you are looping over (unless accessed by reference)
  • in your example code $model['dbID'] is number 907 which is not an array, hence the error message

Found it - I needed to use a reference to $model in order to update it

// Loop thru model entries
$modelList = json_decode($_POST["modelList"], TRUE); 
foreach($modelList as &$model) {

    (do some work)

    // Add the new element
    $model['dbID'] = $newID;
}
unset($model);

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