By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,409 Members | 1,604 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,409 IT Pros & Developers. It's quick & easy.

insert an element in numbered array

P: n/a
Hello,
I need to do the following:

Say I have a multidim array:

$array = array();

$array[0]['name'] = 'Kevin';
$array[0]['age'] = 23;

$array[1]['name'] = 'Julia';
$array[1]['age'] = 31;

$array[2]['name'] = 'Bob';
$array[2]['age'] = 26;

$array[3]['name'] = 'Drew';
$array[3]['age'] = 37;

and I have a seperate array:

$person = array();
$person['name'] = 'Dana';
$person['age'] = 33;

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.

Does php have a built-in function that does this?
..soma
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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/
Jul 17 '05 #2

P: n/a
On Wed, 01 Jun 2005 13:29:41 +0200, somaboy mx <no****@fakemail.fk> wrote:
Say I have a multidim array:

$array = array();

$array[0]['name'] = 'Kevin';
$array[0]['age'] = 23; [snip]
and I have a seperate array:

$person = array();
$person['name'] = 'Dana';
$person['age'] = 33;

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.

Does php have a built-in function that does this?


Yes, array_splice.

<pre>
<?php
$array = array();

$array[0]['name'] = 'Kevin';
$array[0]['age'] = 23;

$array[1]['name'] = 'Julia';
$array[1]['age'] = 31;

$array[2]['name'] = 'Bob';
$array[2]['age'] = 26;

$array[3]['name'] = 'Drew';
$array[3]['age'] = 37;

$person = array();
$person['name'] = 'Dana';
$person['age'] = 33;

print_r($array);
print_r($person);

array_splice($array, 1, 0, array($person));

print_r($array);
?>
</pre>

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #3

P: n/a
On Wed, 01 Jun 2005 13:42:15 +0100, Andy Hassall wrote:
Yes, array_splice.


Ah damn, overlooked :)
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #4

P: n/a
Andy Hassall wrote:
Yes, array_splice.


Thanks! I was afraid that array_splice would merely replace the element
at the given index but obviously it works.

thanks again
..soma
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.