Page 1 of 1

Insert name into an array

Posted: Fri Jul 06, 2012 4:55 pm
by gprince66
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.

Code: Select all

$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"); 

Re: Insert name into an array

Posted: Fri Jul 06, 2012 5:48 pm
by gprince66
Solved

Code: Select all

$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);