Asked  7 Months ago    Answers:  5   Viewed   48 times

I was wondering if it is possible to edit the current object that's being handled within a foreach loop

I'm working with an array of objects $questions and I want to go through and look for the answers associated with that question object in my db. So for each question go fetch the answer objects and update the current $question inside my foreach loop so I can output/process elsewhere.

foreach($questions as $question){
    $question['answers'] = $answers_model->get_answers_by_question_id($question['question_id']);
}

 Answers

91

There are 2 ways of doing this

foreach($questions as $key => $question){
    $questions[$key]['answers'] = $answers_model->get_answers_by_question_id($question['question_id']);
}

This way you save the key, so you can update it again in the main $questions variable

or

foreach($questions as &$question){

Adding the & will keep the $questions updated. But I would say the first one is recommended even though this is shorter (see comment by Paystey)

Per the PHP foreach documentation:

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

Wednesday, March 31, 2021
 
pocketfullofcheese
answered 7 Months ago
92

If you could index the array by gear or some unique value, it would be a lot easier.

$indexed = array();

// create an array using 'gear' as the index
foreach($arrayValue as $value) {
    $indexed[$value['gear']] = $value;
}

// loop over each object
foreach($objectArray as $obj) {
    $value = $indexed[$obj->gear]; // find the corresponding array
    foreach($value as $name => $val) {
        $obj->$name = $val; // assign each array index/value pair to the object
    }
}

If possible to get your code to return the array with the index by default, you can remove the first foreach loop.

Hope that helps.

Wednesday, March 31, 2021
 
wavyGravy
answered 7 Months ago
62

Well, it depends on what the info() method is doing. While you do not provide us such information, we are not able to give you the most accurate answer.

For example, if info method accepts array:

public function info(array $infos) {
    foreach ($infos as $info) {
        // do smth with each element
    }
}

you can pass the array straight to the function:

$check = $adldap->group()->info($array);

if the method accepts only one element, then you need to loop through the array and pass to the method:

foreach ($array as $value) {
    $check = $adldap->group()->info($value);
}

If the method accepts a lot of values at once, i.e. comma separated, you can implode the array

 $check = $adldap->group()->info(implode(',' ,$array));
Saturday, May 29, 2021
 
Litty
answered 5 Months ago
86

If you're using PHP 5.5+, you can use array_column(), like so:

$result = array_column($foo, 'type');

If you want an array with numeric indices, use:

$result = array_values(array_column($foo, 'type'));

If you're using a previous PHP version and can't upgrade at the moment, you can use the Userland implementation of array_column() function written by the same author.

Alternatively, you could also use array_map(). This is basically the same as a loop except that the looping is not explicitly shown.

$result = array_map(function($arr) {
   return $arr['type'];
}, $foo);
Wednesday, July 28, 2021
 
godot
answered 3 Months ago
25

Do you mean something like:

foreach($_POST['something'] as $key => $something) { 
    $example = $_POST['example'][$key];
    $query = mysql_query("INSERT INTO table (row, row2) VALUES ('{$something}','{$example}')"); 
} 
Wednesday, August 11, 2021
 
PeanutsMcgee
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :