Split an array into multiple arrays

Hi,

I have below an array I want to split in three separate arrays. One array contains the username, second array contains Firstname and the thrid array contains Lastname.

 How can I achieve that?

Array ( [0] => Array ( [username] => admin [firstname] => Portal [lastname] => Administrator ) [1] => Array ( [username] => vipin [firstname] => Vipin [lastname] => Kumar ) )

Open in new window


Thanks in advance.
LVL 1
Vipin KumarSr. Network EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian TaoSenior Business Solutions ConsultantCommented:
There's no builtin function for it.  You'll have to do it yourself, with something like:
$usernameArray = array();
$firstnameArray = array();
$lastnameArray = array();

// assume that your current array is named $currentArray
for ($i = 0, $j = count($currentArray); $i < $j; $i++){
  array_push($usernameArray, $currentArray[$i]["username"]);
  array_push($firstnameArray, $currentArray[$i]["firstname"]);
  array_push($lastnameArray, $currentArray[$i]["lastname"]);
}
// now you have them in separate array

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mukesh YadavFull Stack DeveloperCommented:
Try this
/* Your array */
$arr = array(
  array(
    "username"=>"admin",
    "firstname"=>"Portal",
    "lastname"=>"Administrator"),
  array(
    "username"=>"vipin",
    "firstname"=>"Vipin",
    "lastname"=>"Kumar"));

/* New arrays */
$usernames = array();
$firstnames = array();
$lastnames = array();
foreach($arr as $user){  
  $usernames[] = $user["username"];
  $firstnames[] = $user["firstname"];
  $lastnames[] = $user["lastname"];
}

Open in new window

Brian TaoSenior Business Solutions ConsultantCommented:
Or with this code for (somewhat) better performance:
$usernameArray = array();
$firstnameArray = array();
$lastnameArray = array();

// assume that your current array is named $currentArray
for ($i = 0, $j = count($currentArray); $i < $j; $i++){
  $usernameArray[] = $currentArray[$i]["username"];
  $firstnameArray[] = $currentArray[$i]["firstname"];
  $lastnameArray[] = $currentArray[$i]["lastname"];
}
// now you have them in separate array

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.