user3552670
user3552670

Reputation: 325

Is it possible to implode two different arrays in PHP?

$firstName = array_unique($name[1]);
$lastName = array_unique($name[0]);
echo "".implode("<br>",First name: $firstName)." ".implode("<br>",Last name: $lastName)."";

I want the output to be like:

First name: $firstName[0] Last name: $lastName[0];
First name: $firstName[1] Last name: $lastName[1];
etc.

How would I do that?

Upvotes: 0

Views: 2182

Answers (3)

mleko
mleko

Reputation: 12233

Why to complicate

$s="";
foreach(array_unique($name) as $name){
    $s .= "<br>First name: {$name[1]} Last name: {$name[0]}";
}

Upvotes: 0

deceze
deceze

Reputation: 522135

echo join('<br>', array_map(
    function ($first, $last) { return "First name: $first Last name: $last"; },
    $firstName,
    $lastName
));

Note that the $firstName and $lastName arrays must be the same length. I'm not sure this is guaranteed in your case.

Upvotes: 0

Sergiu Paraschiv
Sergiu Paraschiv

Reputation: 10153

You could array_combine them first ending up with an associative array and then use the approach explained here.

implode(', ', array_map(function ($v, $k) { return $k . '=' . $v; }, $input, array_keys($input)));

Upvotes: 1

Related Questions