473,322 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

insert an element in numbered array

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
4 1696
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
19
by: John Keeling | last post by:
Dear all, I tried the test program below. My interest is to examine timing differences between insert vs. append & reverse for a list. My results on my XP Python 2.3.4 are as follows:...
0
by: Raed Sawalha | last post by:
I have following code that reads a binary file into array of bytes, after I read the file i need to insert a space in fisrt element of array ,how can I achieve this System.IO.FileStream fs = new...
2
by: mirandacascade | last post by:
O/S: Win2K Vsn of Python: 2.4 Example: <a> <b createAnotherWhenCondition="x"> <c>text for c</c> <d>text for d</d> </b>
6
by: flash | last post by:
write a program that manipulates arrays of integers. The main program should call three functions: Insert, Delete, and Search. The Insert function should call a function Sort that sorts the array. ...
2
by: Gestorm | last post by:
Suppose we have an array a, the idea is: build another array, int next, for each 0<i<N, next = next position of a in the sorted array, if a is the max, then next is undefined. For example, let the...
2
by: subramanian100in | last post by:
In ISO/IEC 14882:2003 document, in the section '23.2.1.3 deque modifiers', the following is mentioned: iterator insert(iterator position, const T& x); void insert(iterator position, size_type...
2
bugboy
by: bugboy | last post by:
Hi, I have a very long array with sequential numeric keys. What is the best method for inserting an element in the middle thus bumping all following elements along one key.. Like the function...
4
by: ggoubb | last post by:
The purpose of the Insert function is to add a new integer in the Heap assuming that it is not already full. If Heap capacity has been reached, it attempts to double the current capacity. If capacity...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.