Reply to Re: insert an element in numbered array

Your name:

Reply:


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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация