Did you test your function? Your code throws syntax errors (at least in my IDE).
You will have to use
array_map instead of
array_walk if you want to return a modified array.
Personally, I try to avoid untyped function- or method- parameters (AKA "mixed"). Sometimes you cannot avoid them, but I got used to write typed parameters which brings some benefit, for example:
Code: Select all
/**
* array_makeSafe
*
* Applies AppGini's makeSafe function to all entries of $data-array
*
* @param array $data
* @param bool $is_gpc
* @return array
*/
function array_makeSafe(array $data, bool $is_gpc = true)
{
return array_map(function ($entry) use ($is_gpc) {
return makeSafe($entry, $is_gpc);
}, $data);
}
}
Note the type-declaration before the variables (like "
array $data") and also note the type declaration of return value (here: array).
This helps me because of syntax-highlighing and intellisense features of my IDE (Visual Studio Code, free). It is more work to write comments on the functions/methods and parameters, but after a couple of years you will be glad to see the docs and comments your have written in the past.
See intellisense features while coding:

- gyEVRiu3GS.gif (157.02 KiB) Viewed 3073 times
Additionally, the IDE shows syntax errors if I did not use the function correctly, for example when passing in a string instead of an array:

- Code_EAqZu24dep.png (10.41 KiB) Viewed 3072 times
Usage
Code: Select all
$data = [
"name" => "Jan's example",
];
var_dump($data);
$data = array_makeSafe($data);
var_dump($data);