php - Modifications to nested array elements do not stick -
i either working through fatigue barrier or else have serious gap in understanding of php. here need do
- i have array (a) of arrays (a)
- i need iterate through outer array and
- for each of inner arrays need add new element, in turn array
- this sort of code churn out many times day , had expected little trouble it. however, surprise whilst can modify a cannot make modifications stick , appear in a
here code
function fillroutenames($routes,$export) { for($i=0;$i < count($routes);$i++) { $route = $routes[$i]; trigger_error(gettype($route));//shows array, expected $disps = $route['d']; $nd = array(); foreach($disps $disp) $nd[] = fxnname($disp,$export); //now have new element want add $route['nd'] = $nd; trigger_error(json_encode($route)); /as expected output shows new element, nd } trigger_error(json_encode($routes)); //but gone - never did $oute['nd'] = $nd
}
there must blindingly obvious here wrong have been unable figure out. hope here spot issue.
thats because $route
copy of inner array. need add reference or use direct index $routes[$i]
. this:
function fillroutenames($routes,$export) { for($i=0;$i < count($routes);$i++) { $route = &$routes[$i];// add reference trigger_error(gettype($route)); $disps = $route['d']; $nd = array(); foreach($disps $disp) $nd[] = fxnname($disp,$export); $routes[$i]['nd'] = $nd;// or use index trigger_error(json_encode($route)); } trigger_error(json_encode($routes)); }
Comments
Post a Comment