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

email(
 [0] => '[email protected]';
 [1] => '[email protected]';
 [2] => '[email protected]';
)

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] => '[email protected]';

)

[2]  => Array (
 [0] => 'teddy';
 [1] => '[email protected]';

)

[3] => Array (
 [0] => 'bear';
 [1] => '[email protected]';

)
1

2 Answers 2

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]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need something like:

$res = array ();
for($i=0;$i<count($username);$i++) {
   $res[$i][0] = $username[$i];
   $res[$i][1] = $email[$i];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.