Note that there are some explanatory texts on larger screens.

plurals
  1. POHard PHP nut : how to insert items into one associate array?
    primarykey
    data
    text
    <p>Yes, this is a bit of a trick question; <strong>one</strong> array (without copies), as opposed to any odd array. Let me explain, so let's start here ;</p> <pre><code>$a = array ( 'one' =&gt; 1, 'two' =&gt; 2, 'three' =&gt; 3, 'four' =&gt; 4, 'five' =&gt; 5, 'six' =&gt; 6 ) ; </code></pre> <p>Pretend that this array is long, over a hundred long. I loop through it, step by step, but at some point (let's make up that this happens at the second item) something happens. Maybe the data is funky. Nevertheless, we need to add some items to it for later processing, and then keep looping through it, without losing the current position. Basically, I would like to do something like this ;</p> <pre><code>echo current ( $a ) ; // 'two' array_insert ( $a, 'four', 'new_item', 100 ) ; echo current ( $a ) ; // 'two' </code></pre> <p>Definition for array_insert is ( $array, $key_where_insert_happens, $new_key, $new_value ) ; Of course $new_key and $new_value should be wrapped in an array wrapper, but that's besides the point right now. Here's what I want to see happening after the above code having ran ; </p> <pre><code>print_r ( $a ) ; // array ( 'one' =&gt; 1, 'two' =&gt; 2, 'three' =&gt; 3, 'four' =&gt; 4, 'new_item' =&gt; 100, 'five' =&gt; 5, 'six' =&gt; 6 ) ; echo current ( $a ) ; // 'two' </code></pre> <p>Whenever you use array_splice, array_slice, array_push or most of the other array fiddling functions, you basically create a copy of the array, and then you can copy it back, but this breaks the reference to the original array and the position as well, and my loop above breaks. I could use direct reference (ie. <code>$a['new_item'] = 'whatever;</code>) or put it at the end, but none of these will insert items into a given position.</p> <p>Any takers? How can I do a true insert into an associative array directly (that's being processed elsewhere)? My only solution so far is to ;</p> <ol> <li>record the position (<code>current()</code>)</li> <li>Do the splice/insert (array_slice)</li> <li>overwrite old array with new ($old = $new)</li> <li>search the new position (first reset() then looping through to find it [!!!!!!])</li> </ol> <p>Surely there's a better, simpler and more elegant way for doing something that's currently kludgy, heavy and poorly hobbled together? Why isn't there a array_set_position ( $key ) function that quickly can help this out, or an array_insert that works directly on the same array (or both)?</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.
 

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