Skip to main content
more specifics, hopefully clearer
Source Link

Yes, this is a bit of a trick question; one array (without copies), as opposed to any odd array. Let me explain, so let's start here ;

$a = array ( 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5, 'six' => 6 ) ;

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 20thsecond 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 loosinglosing the current position. Basically, I would like to do something like this ;

array_insertecho current ( 'the_key_where_I_want_to_insert',$a array) ;  // 'two'
array_insert ( 'new_key'$a, =>'four', $data'new_item', ...100 ) ;
echo current ( $a ) ;  // 'two'

AndDefinition 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 not to shift the current position insee happening after the array.above code having ran ;

print_r ( $a ) ; // array ( 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'new_item' => 100, 'five' => 5, 'six' => 6 ) ;
echo current ( $a ) ;  // 'two'

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. $a['new_item'] = 'whatever;) or put it at the end, but none of these will insert items into a given position.

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 ;

  1. record the position (current())
  2. Do the splice/insert (array_slice)
  3. overwrite old array with new ($old = $new)
  4. search the new position (first reset() then looping through to find it [!!!!!!])

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)?

Yes, this is a bit of a trick question; one array (without copies), as opposed to any odd array. Let me explain, so let's start here ;

$a = array ( 'one' => 1, 'two' => 2, 'three' => 3 ) ;

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 20th 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 loosing the current position. Basically, I would like something like this ;

array_insert ( 'the_key_where_I_want_to_insert', array ( 'new_key' => $data, ... ) ) ;

And I want to not to shift the current position in the array. 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. $a['new_item'] = 'whatever;) or put it at the end, but none of these will insert items into a given position.

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 ;

  1. record the position (current())
  2. Do the splice/insert (array_slice)
  3. overwrite old array with new ($old = $new)
  4. search the new position (first reset() then looping through to find it [!!!!!!])

Surely there's a better, simpler and more elegant way for doing something that's currently kludgy, heavy and poorly hobbled together?

Yes, this is a bit of a trick question; one array (without copies), as opposed to any odd array. Let me explain, so let's start here ;

$a = array ( 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5, 'six' => 6 ) ;

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 ;

echo current ( $a ) ;  // 'two'
array_insert ( $a, 'four', 'new_item', 100 ) ;
echo current ( $a ) ;  // 'two'

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 ;

print_r ( $a ) ; // array ( 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'new_item' => 100, 'five' => 5, 'six' => 6 ) ;
echo current ( $a ) ;  // 'two'

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. $a['new_item'] = 'whatever;) or put it at the end, but none of these will insert items into a given position.

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 ;

  1. record the position (current())
  2. Do the splice/insert (array_slice)
  3. overwrite old array with new ($old = $new)
  4. search the new position (first reset() then looping through to find it [!!!!!!])

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)?

Source Link

Hard PHP nut : how to insert items into one associate array?

Yes, this is a bit of a trick question; one array (without copies), as opposed to any odd array. Let me explain, so let's start here ;

$a = array ( 'one' => 1, 'two' => 2, 'three' => 3 ) ;

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 20th 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 loosing the current position. Basically, I would like something like this ;

array_insert ( 'the_key_where_I_want_to_insert', array ( 'new_key' => $data, ... ) ) ;

And I want to not to shift the current position in the array. 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. $a['new_item'] = 'whatever;) or put it at the end, but none of these will insert items into a given position.

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 ;

  1. record the position (current())
  2. Do the splice/insert (array_slice)
  3. overwrite old array with new ($old = $new)
  4. search the new position (first reset() then looping through to find it [!!!!!!])

Surely there's a better, simpler and more elegant way for doing something that's currently kludgy, heavy and poorly hobbled together?