7

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?

Bibek Aryal
  • 545
  • 9
  • 28
  • Please check the manual for [array_column()](http://php.net/manual/en/function.array-column.php). It doesn't take four arguments, it takes three. The first one being the array, the second the column (one column) to get and the third argument decide what column to use as index. – M. Eriksson Sep 17 '18 at 05:13
  • I went through it, that is why I'm trying to send four arguments as three. – Bibek Aryal Sep 17 '18 at 05:16
  • Ah. Missed that you concatenated the strings for the second argument. That won't work. That's the same as writing `array_column($records, 'first_namelast_name', 'id);`. That will make it look for a column literally called `first_namelast_name`, which doesn't exist. You can't concatenate two columns with this function. Use the code in @shubham715 answer instead. – M. Eriksson Sep 17 '18 at 05:19
  • @shubham715 yes Im going through it right now – Bibek Aryal Sep 17 '18 at 05:22
  • @MagnusEriksson okay will try his code – Bibek Aryal Sep 17 '18 at 05:22

5 Answers5

7

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
)
shubham715
  • 3,324
  • 1
  • 17
  • 27
7

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;
});
Jacob Mulquin
  • 3,458
  • 1
  • 19
  • 22
  • To me, `array_map()` is more appropriate than `array_reduce()`, because the desired output has the same size (count) as the input array. When the number of returned elements is not equal to the input's count, then `array_reduce()` becomes the favored native function. – mickmackusa Jul 27 '22 at 03:26
1

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
)
sandeep soni
  • 303
  • 1
  • 12
  • 1
    A good answer includes an explanation of _why_ the OP should use it. What problem does it solve? How does it solve it? – M. Eriksson Sep 17 '18 at 05:26
  • Try this (code only) answers are low-value on StackOverflow because they do a poor job of educating/empowering the OP and thousands of future researchers. Always include some explanation with EVERY answer that you post. Don't wait 'til next time, please edit this answer. – mickmackusa Sep 17 '18 at 07:32
1

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 );
Pankaj Bisht
  • 986
  • 1
  • 8
  • 27
  • Try this (code only) answers are low-value on StackOverflow because they do a poor job of educating/empowering the OP and thousands of future researchers. Always include some explanation with EVERY answer that you post. Don't wait 'til next time, please edit this answer. – mickmackusa Sep 17 '18 at 07:31
0

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

Delickate
  • 1,102
  • 1
  • 11
  • 17