title: array_splice preserving keys
body: <p>I faced the situation that splicing arrays with preserved-keys, so I made the following function.<br>
  I reached the solution that wrapping each items with array, but there seems to be some memory-inefficient statements.<br>
  Have you any ideas?<br>
  Thank you.</p>
  
  <hr>
  
  <h1><code>array_splice_pk</code></h1>
  
  <p>This preserves keys, differently from <code>array_splice</code>.</p>
  
  <h3>Overview:</h3>
  
  <ul>
  <li><em><code>&amp;$input</code></em> -> same as <code>array_splice</code> one.</li>
  <li><strong><em><code>$key</code></em></strong> -> target key.</li>
  <li><strong><em><code>$use_key_as_offset</code></em></strong> -> use <em><code>$key</code></em> parameter as a <strong>numeric offset</strong>.</li>
  <li><em><code>$length</code></em> -> same as <code>array_splice</code> one.</li>
  <li><em><code>$replacement</code></em> -> same as <code>array_splice</code> one. But you can also provide <strong>key</strong> for each value.</li>
  </ul>
  
  <h3>Code:</h3>
  
  <pre><code>function array_splice_pk(&amp;$input, $key, $use_key_as_offset = false, $length = 0, $replacement = null) {
      if (!is_array($input) || !is_scalar($key)) {
          return array();
      }
      if ($replacement !== null) {
          $replacement = array($replacement);
          if (!is_array($replacement[0])) {
              $replacement = array($replacement);
          }
      }
      $wrapper = array();
      foreach ($input as $k =&gt; $v) {
          $wrapper[] = array($k =&gt; $v);
      }
      $del_key = null;
      foreach ($wrapper as $k =&gt; $v) {
          if ($use_key_as_offset) {
              if ($k === (int)$key) {
                  $del_key = $k;
                  break;
              }
          } else {
              if (key($v) == $key) {
                  $del_key = $k;
                  break;
              }
          }
      }
      if ($del_key === null) {
          return array();
      }
      if ($replacement === null) {
          $wrapper_ret = array_splice($wrapper, $del_key, $length);
      } else {
          $wrapper_ret = array_splice($wrapper, $del_key, $length , $replacement);
      }
      $ret = $input = array();
      foreach ($wrapper_ret as $wrap) {
          list($k, $v) = each($wrap);
          $ret[$k] = $v;
      }
      foreach ($wrapper as $wrap) {
          list($k ,$v) = each($wrap);
          $input[$k] = $v;
      }
      return $ret;
  }
  </code></pre>
  
  <h3>Sample:</h3>
  
  <pre><code>$arr1 = $arr2 = array(
      'one'   =&gt; 'test',
      'two'   =&gt; 'test',
      'three' =&gt; 'test',
      'four'  =&gt; 'test',
  );
  $ret1 = array_splice_pk($arr1, 'three', false, 1, array('fizz' =&gt; '!!!'));
  $ret2 = array_splice_pk($arr2, 2      , true , 1, array('fizz' =&gt; '!!!'));
  
  var_dump('Result1', $arr1, $ret1, 'Result2', $arr2, $ret2);
  </code></pre>
  
  <h3>Result:</h3>
  
  <pre><code>string(7) "Result1"
  array(4) {
    ["one"]=&gt;
    string(4) "test"
    ["two"]=&gt;
    string(4) "test"
    ["fizz"]=&gt;
    string(3) "!!!"
    ["four"]=&gt;
    string(4) "test"
  }
  array(1) {
    ["three"]=&gt;
    string(4) "test"
  }
  string(7) "Result2"
  array(4) {
    ["one"]=&gt;
    string(4) "test"
    ["two"]=&gt;
    string(4) "test"
    ["fizz"]=&gt;
    string(3) "!!!"
    ["four"]=&gt;
    string(4) "test"
  }
  array(1) {
    ["three"]=&gt;
    string(4) "test"
  }
  </code></pre>