I'm trying to unset multiple variables using array_walk()
. Here's my code:
$remove = ['module', 'file', 'data', 'route'];
array_walk($remove, fn(&$v) => unset($v));
However, this results in the following error:
Parse error: syntax error, unexpected 'unset' (T_UNSET) in C:\files\develoment\OrdersApp\config.php
I know there are other approaches to unsetting multiple variables but it seems like array_walk()
ought to be able to do it. Why is the presence of unset()
in the code above causing a parse error? How should it be modified so it works?
CodePudding user response:
Only the values of the array may potentially be changed; its structure cannot be altered, i.e., the programmer cannot add, unset or reorder elements. If the callback does not respect this requirement, the behavior of this function is undefined, and unpredictable.
$remove = ['module', 'file', 'data', 'route'];
$limit = sizeof($remove);
for($i = 0; $i < $limit ; $i ){
unset($remove[$i]);
}
var_dump($remove);