Concatenate two columns into one using array_column

Viewed 7637

I have an array of type

$records = array(
array(
    'id' => 2135,
    'first_name' => 'John',
    'last_name' => 'Cena',
),
array(
    'id' => 5623,
    'first_name' => 'Peter',
    'last_name' => 'Doe',
));

I want to display the output in the format of

[2135] => John Cena
[5623] => Peter Doe

I have been using following code to try to display my expected result but its not working.

$names = array_column($records, 'first_name' . 'last_name', 'id');

How can I concatenate two columns of array into one?

5 Answers

You can create a new array by using foreach loop.

Try this

  $newArray = [];
    foreach($records as $key => $value) {
      $newArray[$value['id']]= $value['first_name']." ".$value['last_name'];          
    }

   print_r($newArray); 

Output

Array
(
    [2135] => John Cena
    [5623] => Peter Doe
)

The reason your current code is not working is because you are asking PHP to look in the $records array for the key first_namelast_name, which doesn't exist.

array_column only returns a single column, as stated in the PHP manual entry

(PHP 5 >= 5.5.0, PHP 7)

array_column — Return the values from a single column in the input

You need to loop through the array and concatenate the strings manually. One function you can use is array_reduce:

$names = array_reduce($records, function ($result, $item) {
    $result[$item['id']] = $item['first_name'] . ' ' . $item['last_name'];
    return $result;
});

You can achieve it by creating the another array. Simply merge the value of sub-array in new created array using foreach loop.

$records = array(
array(
    'id' => 2135,
    'first_name' => 'John',
    'last_name' => 'Cena',
),
array(
    'id' => 5623,
    'first_name' => 'Peter',
    'last_name' => 'Doe',
));

foreach($records as $data)
{
  $temp[$data['id']] = $data['first_name'].' '.$data['last_name'];
}
echo "<pre>";
print_r($temp);



// Output

Array
(
    [2135] => John Cena
    [5623] => Peter Doe
)

array_column works on that kind of problem but here it can't concatenate first_name and last_name keys so You can also try this code -

$records = array(
array(
    'id' => 2135,
    'first_name' => 'John',
    'last_name' => 'Cena',
),
array(
    'id' => 5623,
    'first_name' => 'Peter',
    'last_name' => 'Doe',
));

$tmp = [];

foreach($records as $key) {
    $tmp[$key["id"]] = $key["first_name"] . " " . $key["last_name"];
}

print_r($tmp); // Array ( [2135] => John Cena [5623] => Peter Doe );

There is no php builtin function to do that i guess. You may loop through array and concatenate them.

$records = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Cena',
),
array(
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe', 
));

$newArray = array();
foreach($records as $data)
{
$index = "";
$value = "";
$indexLoop = 1;
foreach($data as $reckey => $recValue)
{
    if($indexLoop ==1)
    {
        $index = $recValue; 
    }else{
            $value .= $recValue." ";
         }  

    $indexLoop++;
}

$newArray[$index] = $value;
}

 echo "<pre>"; print_r($newArray);

Thanks

Related