1
username(
 [0] => 'andrew';
 [1] => 'teddy';
 [2] => 'bear';
)

email(
 [0] => 'andrew@andrew.com';
 [1] => 'teddy@teddy.com';
 [2] => 'bear@bear.com';
)

I got 2 Array coming in from post. I am processing this with PHP. I would like to combine the array so it looks like this. So I can use a loop on the array to insert a query on a database.

[1] => Array (
 [0] => 'andrew';
 [1] => 'andrew@andrew.com';

)

[2]  => Array (
 [0] => 'teddy';
 [1] => 'teddy@teddy.com';

)

[3] => Array (
 [0] => 'bear';
 [1] => 'bear@bear.com';

)
momoterraw
  • 165
  • 2
  • 6
  • 15
  • 2
    http://stackoverflow.com/questions/4072348/php-merging-arrays-with-common-keys this will solve all your problems :) – Samuel Cook Nov 21 '12 at 01:44

2 Answers2

1

Take a look at array_combine()

If that doesn't solve your problem, you can always just go with a simple loop:

foreach($usernameArray as $k=>$val)
{
    if(array_key_exists($k, $emailArray))
    {
        $combinedArray[$k] = array($val, $emailArray[$k]);
    }
}
Alec Sanger
  • 4,442
  • 1
  • 33
  • 53
0

You need something like:

$res = array ();
for($i=0;$i<count($username);$i++) {
   $res[$i][0] = $username[$i];
   $res[$i][1] = $email[$i];
}
Hernan Velasquez
  • 2,770
  • 14
  • 21