Insert name into an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Insert name into an array

Postby gprince66 » Fri Jul 06, 2012 4:55 pm

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.

Syntax: [ Download ] [ Hide ]
$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");
gprince66
Forum Newbie
 
Posts: 6
Joined: Wed Feb 15, 2012 12:55 am

Re: Insert name into an array

Postby gprince66 » Fri Jul 06, 2012 5:48 pm

Solved

Syntax: [ Download ] [ Hide ]
$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);
gprince66
Forum Newbie
 
Posts: 6
Joined: Wed Feb 15, 2012 12:55 am


Return to PHP - Code

Who is online

Users browsing this forum: No registered users and 5 guests