title: Alternatives to Pass both Key and Value By Reference:
body: <p>Can someone explain to me why you can't pass a key as reference?</p>
  
  <p>Ex:</p>
  
  <pre><code>if(is_array($where)){
      foreach($where as &amp;$key =&gt; &amp;$value){
          $key = sec($key);
          $value = sec($value);
      }
      unset($key, $value);
  }
  </code></pre>
  
  <p>Throws:</p>
  
  <pre><code>Fatal error: Key element cannot be a reference in linkstest.php on line 2   
  </code></pre>
  
  <p>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.</p>
  
  <p>Array map is difficult for me to understand:</p>
  
  <p>I have tried many things with array_map, but I can't get it to act on the keys directly.</p>
  
  <p>Would I get any performance benefit using array map than just using a foreach loop?</p>
  
  <p>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:</p>
  
  <pre><code>foreach($where as $key =&gt; $value){
   $where[secure($key)] = secure($value);
  }
  </code></pre>
  
  <p>This might fail if it finds something to escape in the key, adding a new element, and keeping the unescaped one.</p>
  
  <p>So am I stuck with something like this?</p>
  
  <pre><code>$temparr = array();
  foreach($where as $key =&gt; $value){
   $temparr[secure($key)] = secure($value);
  }
  $where = $temparr;
  unset($temparr);
  </code></pre>
  
  <p>Any alternatives?</p>