Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Maybe I'm not understanding you correctly but have you looked into <a href="http://php.net/manual/en/function.array-splice.php" rel="nofollow noreferrer"><code>array_splice()</code></a>?</p> <p>This <a href="https://stackoverflow.com/questions/2795987/array-splice-numerical-offsets-of-associative-arrays/2796067#2796067">answer</a> might also interest you.</p> <hr> <p><s> Would something like this work?</p> <pre><code>function array_insert($input, $key, $value) { if (($key = array_search($key, array_keys($input))) !== false) { return array_splice($input, $key, 1, $value); } return $input; } </code></pre> <p></s></p> <hr> <p>This was the best I could come up with:</p> <pre><code>$a = array ( 'one' =&gt; 1, 'two' =&gt; 2, 'three' =&gt; 3, 'four' =&gt; 4, 'five' =&gt; 5, 'six' =&gt; 6, ); ph()-&gt;Dump(next($a)); // 2 array_insert($a, 'four', array('new_item' =&gt; 100)); ph()-&gt;Dump(current($a)); // 2 function array_insert(&amp;$array, $key, $data) { $k = key($array); if (array_key_exists($key, $array) === true) { $key = array_search($key, array_keys($array)) + 1; $array = array_slice($array, null, $key, true) + $data + array_slice($array, $key, null, true); while ($k != key($array)) { next($array); } } } ph()-&gt;Dump($a); /* Array ( [one] =&gt; 1 [two] =&gt; 2 [three] =&gt; 3 [four] =&gt; 4 [new_item] =&gt; 100 [five] =&gt; 5 [six] =&gt; 6 ) */ </code></pre> <p>I don't think it's possible to set an array internal pointer <a href="http://pt.php.net/manual/en/function.next.php#83087" rel="nofollow noreferrer">without looping</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload