Can someone explain to me why you can't pass a key as reference?
Ex:
if(is_array($where)){
foreach($where as &$key => &$value){
$key = sec($key);
$value = sec($value);
}
unset($key, $value);
}
Throws:
Fatal error: Key element cannot be a reference in linkstest.php on line 2
Can I do something similar using array_map? All I want to do is iterate over an associative array, and escape both the key and value with my sec() function.
Array map is difficult for me to understand:
I have tried many things with array_map, but I can't get it to act on the keys directly.
Would I get any performance benefit using array map than just using a foreach loop?
What I don't like about foreach is that I can't act on the array directly, and have to deal with creating temporary arrays and unsetting them:
foreach($where as $key => $value){
$where[secure($key)] = secure($value);
}
This might fail if it finds something to escape in the key, adding a new element, and keeping the unescaped one.
So am I stuck with something like this?
$temparr = array();
foreach($where as $key => $value){
$temparr[secure($key)] = secure($value);
}
$where = $temparr;
unset($temparr);
Any alternatives?
sec($key)
results in the value of another existing key? Do you just overwrite the existing key? You need to explain why in your application a key could be dangerous. – Phelloderm