0
$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?

user3552670
  • 305
  • 2
  • 3
  • 10

3 Answers3

1

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)));

Community
  • 1
  • 1
Sergiu Paraschiv
  • 9,929
  • 5
  • 36
  • 47
0
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.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

Why to complicate

$s="";
foreach(array_unique($name) as $name){
    $s .= "<br>First name: {$name[1]} Last name: {$name[0]}";
}
mleko
  • 11,650
  • 6
  • 50
  • 71