Insert name into an array

Hi all,

I have an array of several hundred names. I need to insert the name of spouse into the array.

How do I insert Jane Doe after John Doe?

Thanks in advance for your help.

[php]$array = (“John Smith”, “John Doe”, “John Jones”);

$spouse1 = “John Doe”;
$spouse2 = “Jane Doe”;

// I want array to be…
// $array = (“John Smith”, “John Doe”, “Jane Doe”, “John Jones”); [/php]

This works for me:

[php]$array = array(“John Smith”, “John Doe”, “John Jones”);

$spouse1 = “John Doe”;
$spouse2 = “Jane Doe”;

function insert_after($array,$search,$add)
{
$key = array_search($search,$array);
if($key !== false)
{
array_splice($array,$key+1,0,array($add));
}
return $array;
}
$array = insert_after($array, $spouse1, $spouse2);[/php]

array_search = http://uk3.php.net/array_search
array_splice = http://uk3.php.net/array_splice

works perfectly. Thank you so much!

Sponsor our Newsletter | Privacy Policy | Terms of Service