<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>BinaryKitten&#039;s Development Dropbox &#187; insert</title>
	<atom:link href="http://binarykitten.com/tag/insert/feed" rel="self" type="application/rss+xml" />
	<link>http://binarykitten.com</link>
	<description>Curently a work in progress. Please be patient</description>
	<lastBuildDate>Thu, 27 Oct 2011 21:49:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>php: Insert element at specific Index of Array.</title>
		<link>http://binarykitten.com/dev/php/52-php-insert-element-and-shift.html</link>
		<comments>http://binarykitten.com/dev/php/52-php-insert-element-and-shift.html#comments</comments>
		<pubDate>Sun, 11 Jan 2009 13:41:43 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[insert]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=52</guid>
		<description><![CDATA[In a recent project I&#8217;ve been undertaking I&#8217;ve made a menu class set and sometimes i just wanted to say to add this to an array, but in a certain position. PHP does this easily via $array[position] = &#34;newdata&#34;; But unfortunately this is not so good if you only wanted to insert the newdata into [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent project I&#8217;ve been undertaking I&#8217;ve made a menu class set and sometimes i just wanted to say to add this to an array, but in a certain position. PHP does this easily via</p>
<pre class="brush: php">
$array[position] = &quot;newdata&quot;;
</pre>
<p>But unfortunately this is not so good if you only wanted to insert the newdata into that position and move the others the out of the way. So with a little help from <a title="Derick's Blog" href="http://derickrethans.nl/" target="_blank">Derick Rethans</a> and <a title="Tetraboy's Blog" href="http://www.tetraboy.com" target="_blank">Tetraboy</a> I came up with this little solution.</p>
<p>Any comments gratefully recieved.</p>
<pre>
<pre class="brush: php">
function array_insert(&amp;$array,$element,$position=null) {
  if (count($array) == 0) {
    $array[] = $element;
  }
  elseif (is_numeric($position) &amp;&amp; $position &lt; 0) {
    if((count($array)+position) &lt; 0) {
      $array = array_insert($array,$element,0);
    }
    else {
      $array[count($array)+$position] = $element;
    }
  }
  elseif (is_numeric($position) &amp;&amp; isset($array[$position])) {
    $part1 = array_slice($array,0,$position,true);
    $part2 = array_slice($array,$position,null,true);
    $array = array_merge($part1,array($position=&gt;$element),$part2);
    foreach($array as $key=&gt;$item) {
      if (is_null($item)) {
        unset($array[$key]);
      }
    }
  }
  elseif (is_null($position)) {
    $array[] = $element;
  }
  elseif (!isset($array[$position])) {
    $array[$position] = $element;
  }
  $array = array_merge($array);
  return $array;
}
</pre>
</pre>
<p>and no code would be without it&#8217;s example:</p>
<pre class="brush: php">
// create the array
$x = array(&quot;apples&quot;,&quot;bananas&quot;,&quot;pears&quot;);
//insert &quot;oranges&quot; at position 1
array_insert($x,&quot;oranges&quot;,1);
var_dump($x);
//insert &quot;pineapples&quot; 2 from the end
array_insert($x,&quot;pineapples&quot;,-2);
var_dump($x);
//insert &quot;strawberries&quot; at the end
array_insert($x,&quot;strawberries&quot;);
var_dump($x);
//insert &quot;plums&quot; at position 0 - (because the negative position goes beyond 0)
array_insert($x,&quot;pineapples&quot;,-10);
var_dump($x);
</pre>
<p>or alternatively</p>
<pre class="brush: php">
// create the array
$x = array(&quot;apples&quot;,&quot;bananas&quot;,&quot;pears&quot;);
//insert &quot;oranges&quot; at position 1
$x = array_insert($x,&quot;oranges&quot;,1);
var_dump($x);
//insert &quot;pineapples&quot; 2 from the end
$x = array_insert($x,&quot;pineapples&quot;,-2);
var_dump($x);
//insert &quot;strawberries&quot; at the end
$x = array_insert($x,&quot;strawberries&quot;);
var_dump($x);
//insert &quot;plums&quot; at position 0 - (because the negative position goes beyond 0)
$x = array_insert($x,&quot;plums&quot;,-10);
var_dump($x);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/php/52-php-insert-element-and-shift.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

