How to convert submitted form array to this array on php?

Lets suppose i have the following array submitted by a html form:

array(
    'firstname' => array('Sara','Jim'),
    'lastname'  => array('Gibson','Jobs')
);

What i wanna achieve is the following array:

array(
    array(
        'firstname' => 'sara',
        'lastname'  => 'Gibson'
    ),
    array(
        'firstname' => 'Jim',
        'lastname'  => 'Jim'
    )
);

I need a function to automatically sort the array not manually by entering data but automatically processing array data.

Answers


I don't see anything wrong with @Tim Cooper's solution, but if you don't want to mention the keys manually, you can also use:

$new_arr = array();
foreach ($arr as $key => $value)
{
  foreach ($value as $numkey => $value2)
  {
    $new_arr[$numkey][$key] = $value2;
  }
}

$new_arr = array();
$length = count($arr['firstname']);
for($i = 0; $i < $length; $i++)
{
   $new_arr[] = array('firstname' => $arr['firstname'][$i], 'lastname' => $arr['lastname'][$i]);
}

Need Your Help

using the fql code with fb.api

javascript facebook facebook-graph-api

hey guys i am new to javascript facebook sdk and am trying to generate the photos on the profile picture album.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.