-5

I am changing variable values ($eachRelationalColumn["columnFiler"]["enabled"], shows in the picture) in a foreach loop. The variables' value can be changed successfully within the foreach loop (See the second line and fourth line on the picture). however, once the variables leave the foreach loop, then the varibles become the original value which does not get changed (As highlighted in the picture, both enabled values remain true (1) instade of becoming asdsad or falsadsadasdse ).

output

   case "relationalColumnfilter":

          foreach ($_SESSION["config_arr"]['tableOptions']['relational_columns']['columns'] as $eachRelationalColumn) {

            if ($eachRelationalColumn["displayColumn"]) {

              if ($eachRelationalColumn["columnFiler"]["enabled"]) {
                
                //check if displayed relational option is in the selected column filter
                //if it is in the selected column filer then change the selected to true
                //if it is not in the selected column filter then change the selected to false
                if (in_array($eachRelationalColumn["columns_name"]["label"], $setting['value'])) {
                  echo "<br>" . $eachRelationalColumn["columns_name"]["label"] . " is selected " . "<br>";
                  $eachRelationalColumn["columnFiler"]["enabled"] = "asdsad";
                  echo $eachRelationalColumn["columnFiler"]["enabled"];
                } else {
                  echo "<br>" . $eachRelationalColumn["columns_name"]["label"] . " is unselected " . "<br>";
                  $eachRelationalColumn["columnFiler"]["enabled"] = "falsadsadasdse";
                  echo $eachRelationalColumn["columnFiler"]["enabled"];
                }


              }
              

            }

          }

          echo "<pre>";
          print_r($_SESSION["config_arr"]['tableOptions']['relational_columns']['columns']);
          echo "<pre>";
David
  • 11
  • 5

1 Answers1

1

In order for array values to change inside loop, you must add reference & to that item:

foreach ($_SESSION["config_arr"]['tableOptions']['relational_columns']['columns'] as &$eachRelationalColumn) {
    $eachRelationalColumn = 'fooBar';
}
Justinas
  • 41,402
  • 5
  • 66
  • 96