|
Posted by Ewoud Dronkert on 06/01/05 15:17
On Wed, 01 Jun 2005 13:29:41 +0200, somaboy mx wrote:
> now I want to insert the $person array into the $array in such a way
> that it ends up at index 1 and the subsequent elements get pushed up and
> renumbered, so $array[1] becomes $array[2] and so on.
No one built-in function, but you could use array_slice and array_merge:
$p = array( 'name' => 'John', 'age' => 42 );
$insertat = 1;
if ( $insertat <= 0 )
{
$a = array_merge( array( $p ), $a );
}
elseif ( $insertat >= count( $a ) )
{
$a[] = $p;
}
else
{
$a0 = array_slice( $a, 0, $insertat );
$a1 = array_slice( $a, $insertat );
$a = array_merge( $a0, array( $p ), $a1 );
}
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
[Back to original message]
|