user968808
user968808

Reputation:

php array, splitting 1 array in to 2 variables

I have an array that is used to get a persons name and i have the variable below which will give me the Full name name the user. e.g. John Smith. How do i split this array in to two variables.

e.g. $fb_firstname and $fb_lastname?

$fb_name = $user_profile['name'];

Upvotes: 1

Views: 75

Answers (3)

hakre
hakre

Reputation: 197732

You can use explode­Docs for that and a space as delimiter:

$fb_name = $user_profile['name'];
list($fb_firstname, $fb_lastname) = explode(' ', $fb_name, 2) + array('', '');

The important part with that code is that the name needs to have one space only between first and last name and that the firstname comes first.

If there is no lastname, $fb_lastname will be an empty string.

Upvotes: 1

Deept Raghav
Deept Raghav

Reputation: 1460

use the explode function. and then assign output array [0] element as $fb_firstname and [1] element as $fb_lastname.

Upvotes: 1

Logan Serman
Logan Serman

Reputation: 29870

$arr = explode(' ', $fb_name);
$fb_firstname = $arr[0];
$fb_lastname = $arr[1];

Upvotes: 2

Related Questions