Jump to content

preg_split and arrays problem


trums

Recommended Posts

Hi guys, Here I have a first and last name separated by a space. I want to split the name up into first and last at the space. I tried writing the following, however it doesn't work with a variable in the index of $nameArray[]. Can anyone see how I can fix this, or suggest an alternative? thanks!

 

$i = 0;
while($i <= $numGuests){
$splitName = preg_split("/ /", $nameArray[$i]);
echo $splitName[0].'  '.$splitName[1];
        $i++;
}

Link to comment
Share on other sites

Sure. Here is what I'm trying to input:

// Name array is basically an array of full names. (not necessarily ending at 2)
$nameArray = ( 0 => 'John Doe', 1 => 'Mary Jane', 2 => 'Bob Smith');

// $numGuests is the number of names in the $nameArray. For this instance, 2.
$numGuests = 2; 

$i // Dummy var.

 

I know I could just use a foreach loop instead of using a dummy var and a while loop, however I used a while because $i and $numGuests are used elsewhere in the while. ( I have only included the part that isn't working. This piece of code doesn't depend on anything that hasn't been included. )

Link to comment
Share on other sites

Actually you have 3 elements in this array:

<?php
$nameArray = ( 0 => 'John Doe', 1 => 'Mary Jane', 2 => 'Bob Smith');
?>

 

If already have the names in the format you want them. Why bother splitting the values and recombining them.

 

BTW, you didn't show us your complete code that is giving you problems.

 

Ken

Link to comment
Share on other sites

Actually you have 3 elements in this array:

<?php
$nameArray = ( 0 => 'John Doe', 1 => 'Mary Jane', 2 => 'Bob Smith');
?>

Whoops, sorry, I added up wrong. $numGuests should = 3.

 

I'm splitting them so I can insert them into a database, recombining and echoing them is purely for testing.

 

This is the only piece that is giving me problems.. but just in case here is the whole function:


the name POST is full names separated by commas eg: Jane doe,Mark Smith,John Doe
the email POST is email addresses separated by commas.

	$nameArray = explode(',', $this->input->POST('name',true));
	$emailArray = explode(',', $this->input->POST('email',true));
	$eventId = $this->input->POST('eventid',true);
	$ePid = $this->input->POST('ePid',true);
	$numGuests = $this->input->POST('guests',true);
	$i = 0;
	while($i <= $numGuests){
		$splitName = preg_split("/ /", $nameArray[$i];
		$data['stuff'] = $nameArray[0].' / '.$nameArray[1].'<br/>'.$splitName[0].' + '.$splitName[1];
		$additional_data = array(
			'first_name' => $splitName[0],
			'last_name' => $splitName[1],
			'email_rvsp' => "1",
			'email_message' => "1",
			'email_pic' => "1",
			'email_invite_other' => "0",
		);
		        $id = $this->ion_auth->register(null, base64_decode($ePid), $emailArray[$i], $additional_data, 'EGuest');
			$guestData = array('event_id' => $eventId, 'user_id' => $id);
			$this->db->insert('guests', $guestData); 
		$i++;
	}

 

Just in case I was confusing above, the problem I am encountering is that if I put $nameArray[$i]; into the preg_split() statement, it states that $splitName[1] does not exist and $splitName[0] is empty . However, if I put $nameArray[0]; it all works fine.

 

I was originally using explode(), however it gives me the same problem

 

Thanks for the help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.