Home > Software engineering >  foreach loop does not return all array results
foreach loop does not return all array results

Time:03-01

I have the below code but I'm not getting all the results from the array

  foreach ($reviewers as $login_name => $common_name) {
    $allowed_values = [
      '' => 'All',
      $login_name => $common_name,
    ];
  }

CodePudding user response:

You overwrite the entire array each time through the loop. Just add the key and value in the loop:

  $allowed_values = ['' => 'All'];

  foreach ($reviewers as $login_name => $common_name) {
      $allowed_values[$login_name] = $common_name;
  }
  • Related