Re: Insert new array after specific key in multidimensional array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 02/27/2012 08:12 PM, Micky Hulse wrote:
> Howdy!
> 
> Example code:
> 
> <https://gist.github.com/1928452>
> 
> What would be the best way to insert $o_insert array into $o array
> after specified key?
> 
> I hate to just ask for example code, but I can't seem to find the
> perfect solution. :(
> 
> Many thanks in advance for the help!
> 
> Cheers,
> Micky


Might be an easier way but this should work.  You can sort the $before =
true out for yourself :-)

function array_insert(&$array, $key, $insert, $before = FALSE) {
    $i = 0;
    foreach($array as $k => $v) {
      if($k === $key) {
	$p = $i + 1;
	break;
      }
      $i++;
    }
    $array = array_merge(array_slice($array, 0, $p), $insert,
array_slice($array, $p, count($array) - $p));
}

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux