3 Replies - 1904 Views - Last Post: 19 March 2012 - 12:00 PM

#1 pmiller624   User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 02-May 10

Array Insertion

Posted 09 May 2010 - 05:42 PM

Description: $array = the array you want the value to be inserted into

$value = the value that you want to be inserted

$index = the position in the array you want it to be inserted into (remember the starting index of an array is 0)Inserts a value into an array at a specified index. Moves all values that come after the specified index back one.
function arrayInsert($array, $value, $index){
        //Loops through array pushing everything back one
	for($i = count($array)-1; $i >= $index; $i--)
		$array[$i+1] = $array[$i];
        //Inserts the new value
	$array[$index] = $value;
	return $array;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Array Insertion

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5239
  • View blog
  • Posts: 14,033
  • Joined: 18-April 07

Re: Array Insertion

Posted 15 May 2010 - 03:49 PM

Pretty simple, but still useful. :)
Was This Post Helpful? 0
  • +
  • -

#3 Angelsmadream   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 20-January 11

Re: Array Insertion

Posted 24 January 2011 - 05:14 AM

yes really useful .. :)
Was This Post Helpful? 0
  • +
  • -

#4 Cha0sBG   User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 167
  • Joined: 09-April 09

Re: Array Insertion

Posted 19 March 2012 - 12:00 PM

Thank you, exactly what i was searching for :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1