Page 1 of 1

Array question....

Posted: Wed Dec 15, 2004 4:52 pm
by Todd_Z
If i have an array... lets say

Code: Select all

<?php
$ar = array ( "One", "Two", "Four", "Five" );
?>
How do I put "Three" in between two and four?

Posted: Wed Dec 15, 2004 6:35 pm
by John Cartwright
from php.net
I needed a function to add data to a particular place in an array without loosing any other data in the array. Here it is:

Code: Select all

<?php
function insert_into_array($array,$ky,$val) 
{ 
 $n = $ky; 
 foreach($array as $key => $value) 
   { 
     $backup_array[$key] = $array[$key]; 
   } 
 $upper_limit = count($array); 
 while($n <= $upper_limit) 
   { 
     if($n == $ky) 
       { 
     $array[$n] = $val; 
     echo $n; 
       } 
     else 
       { 
     $i = $n - "1"; 
     $array[$n] = $backup_array[$i]; 
       } 
     $n++; 
   } 
 return $array; 
} 
?>