473,394 Members | 1,828 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,394 software developers and data experts.

Inserting a value into the middle of an array: Is this the best way?

I'm trying to insert a value into the middle of a simple
(numerically-ordered) array, and bump all the later array keys up one.
AFAIK, there isn't a function to do this already, so this is the code I
came up with... is this the best way of doing it?

<?php
function array_insert($array, $value, $position)
{
if (is_array($array)) {
$array_out = $array; // so I don't mangle it during foreach
foreach ($array as $key => $val) {
if ($key < $val) { $array_out[$key] = $val; }
else { $array_out[$key+1] = $val; }
}
$array_out[$position] = $value;
return $array_out;
}
return false;
}
?>

--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com
Jul 17 '05 #1
3 2484
"FLEB" <so*********@mmers.and.evil.ones.will.bow-down-to.us> wrote in
message news:1t*****************************@40tude.net...
I'm trying to insert a value into the middle of a simple
(numerically-ordered) array, and bump all the later array keys up one.
AFAIK, there isn't a function to do this already, so this is the code I
came up with... is this the best way of doing it?


array_splice() doesn't work? Hmmm...
Jul 17 '05 #2
Regarding this well-known quote, often attributed to Chung Leong's famous
"Sun, 18 Jul 2004 20:57:42 -0400" speech:
"FLEB" <so*********@mmers.and.evil.ones.will.bow-down-to.us> wrote in
message news:1t*****************************@40tude.net...
I'm trying to insert a value into the middle of a simple
(numerically-ordered) array, and bump all the later array keys up one.
AFAIK, there isn't a function to do this already, so this is the code I
came up with... is this the best way of doing it?


array_splice() doesn't work? Hmmm...


I haven't tried it, but according to the docs, array_splice overwrites a
section of an array with another. I'd like to insert without overwriting.

--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com
Jul 17 '05 #3
Regarding this well-known quote, often attributed to FLEB's famous "Mon, 19
Jul 2004 15:53:44 -0400" speech:
Regarding this well-known quote, often attributed to Chung Leong's famous
"Sun, 18 Jul 2004 20:57:42 -0400" speech:
"FLEB" <so*********@mmers.and.evil.ones.will.bow-down-to.us> wrote in
message news:1t*****************************@40tude.net...
I'm trying to insert a value into the middle of a simple
(numerically-ordered) array, and bump all the later array keys up one.
AFAIK, there isn't a function to do this already, so this is the code I
came up with... is this the best way of doing it?


array_splice() doesn't work? Hmmm...


I haven't tried it, but according to the docs, array_splice overwrites a
section of an array with another. I'd like to insert without overwriting.


And, reading further down the page, I find that it does just that. Never
mind, and thanks.

--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: maciej | last post by:
I got an array that consists of elements that are arrays also. Now I wish I could add an element to the middle of it. Let mi give you an example: array ( - array(1,15,apple), -...
16
by: cwizard | last post by:
I'm calling on a function from within this form, and there are values set but every time it gets called I get slammed with a run time error... document.frmKitAmount.txtTotalKitValue is null or not...
1
by: Michael Albanese | last post by:
I am building an application to report on-the-job injuries and incidents. There are a lot of Date fields, some of which are optional and can be left blank by the user. I have allowed Nulls on...
3
by: KL | last post by:
Well, I am back. This time our assignment has us filling a vector and then timing how long it takes to find a spot in the vector to insert a new item, and the time required to insert the item...
5
by: Peter Nagel | last post by:
I would like to insert some strings in a multiline TextBox. At the time of inserting the TextBox already contains some strings each in a separate line. The new lines should be inserted somewhere in...
3
by: JH | last post by:
I have tried both suggested options shown below to insert simple string values to an Access table, but to no avail. The executeNonQuery failed miserably. I got the correct string values from each...
10
by: Ryan Graham | last post by:
I totally bombed this question in an interview so I'm posting my answer here for comments and suggestions... perhaps (god help me) I'm just not that bright, but this works and seems to be fairly...
15
by: Jaraba | last post by:
I am working in a project that I need to parse an arrayt an select records based upon the values parsed. I used the functions developed by Knut Stolze in his article 'Parsing Strings'. I am...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.