PHP array - create an array from an array? -
hello have post array looks this,
array ( [email_address] => array ( [0] => simon@simonainley.info [1] => simon2@simonainley.info ) [firstname] => array ( [0] => simon [1] => simon2 ) [surname] => array ( [0] => ainley [1] => ainley2 ) [companies_company_id] => null, [save_user] => save user )
i wanting create new array first email_address, firstname, , surname array, deal data , proceed next email-address, firstname , surname.
is possible? have tried code,
$newarray = array(); foreach($_post $key => $value) { $newarray[] = $value; }
however code produces this,
array ( [0] => array ( [0] => simon@simonainley.info [1] => simon2@simonainley.info ) [1] => array ( [0] => simon [1] => simon2 ) [2] => array ( [0] => ainley [1] => ainley2 ) [3] => [4] => save user ) 1
what need do?
$count = count($_post['firstname']); $result = array(); ($i = 0; $i <= $count; $i ++) { $result[] = array( 'email_address' => $_post['email_address'][0], 'firstname' => $_post['firstname'][0], 'lastname' => $_post['lastname'][0] ); }
or (if numeric indices have meaning)
$result = array(); foreach (array_keys($_post['email_address']) $index) { $result[$index] = array( 'email_address' => $_post['email_address'][$index], 'firstname' => $_post['firstname'][$index], 'lastname' => $_post['lastname'][$index] ); }
Comments
Post a Comment