0

I have this:

$Array1 = "FirstName, LastName, Email";
$Array2 = "John, Doe, [email protected]";

Using a foreach or other means, Could the final array format to look like this?

$mergedArrays = array(
    'FirstName'        =>"John",
    'LastName'     => "Doe",
    'Email'       =>'[email protected]',
);

print_r($mergedArrays);
2
  • php.net/manual/en/function.array-combine.php Commented Jul 5, 2018 at 20:55
  • print_r(array_combine(explode(', ',$Array1),explode(', ',$Array2))); Commented Jul 5, 2018 at 21:06

2 Answers 2

3

If you are sure that both array will contain the same number separeted by comma, use this:

Version 1 (testing with 100000 takes ~0.08s, +-4x faster)

<?php
$Array1 = "FirstName, LastName, Email";
$Array2 = "John, Doe, [email protected]";

$Array1 = explode(',', preg_replace('/\s*,\s*/',',',$Array1)); //remove spaces before and after comma
$Array2 = explode(',', preg_replace('/\s*,\s*/',',',$Array2));

if(count($Array1) == count($Array2)) {
    $result = array_combine($Array1, $Array2);
}

print_r($result);

Output:

Array (
    [FirstName] => John
    [LastName] => Doe
    [Email] => [email protected]
)

Alternative version from @castis (testing with 100000 takes ~0.3s)

$Array1 = explode(',',$Array1);
$Array2 = explode(',',$Array2);

$result = array_combine(array_map('trim', $Array1), array_map('trim', $Array2));
Sign up to request clarification or add additional context in comments.

12 Comments

One small improve explode(', ',$Array1);
Updated with regex to remove before and after extra space.
would be less confusing to array_combine(array_map(trim, $Array1), array_map(trim, $Array2)); but +1 overall
Updated. The regex code runs faster. Of course, with 3 records makes no difference, but just for curiosity.
"The regex code runs faster" - You don't happen to have the source for those test results? Not saying your test is wrong, I'm just curious. Or they are easy enough to do myself :-)
|
1

If you must use a foreach (where array_combine would do):

<?php

$fields = ['FirstName', 'LastName', 'Email'];
$values = ['John', 'Doe', '[email protected]'];

foreach($fields as $k => $field)
    $result[$field] = $values[$k];

var_export($result);

Output:

array (
    'FirstName' => 'John',
    'LastName' => 'Doe',
    'Email' => '[email protected]',
)

4 Comments

Your input doesn't look like the OP's input. $Array1 and $Array2 would be better labeled $csv1 and $csv2.
@mickmackusa I made an assumption there that the OP had taken a shortcut when writing down their arrays for the question.
If the input really starts out as a string it smells like CSV input. Perhaps str_getcsv could help here.
I would like str_getcsv() here if it allowed two-character delimiters. A bit disappointing that my downvote on the question was neutralized, this undermines my fair action. (No research, no attempt, bad variable naming impacts question clarity.)

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.