Simplest for your specific sample data would be to access the 0
column of data and implode that. (Demo)
echo implode(', ', array_column($array, 0));
Here are a couple of additional techniques to add to this heap...
Reindex the first level because the splat operator doesn't like associative keys, then merge the unpacked subarrays, and implode.
Use array_reduce()
to iterate the first level and extract the first element from each subarray, delimiting as desired without the use of implode()
.
Codes: (Demo)
$array = [
'name' => ['Some message to display1'],
'test' => ['Some message to display2'],
'kudos' => ['Some message to display3']
];
echo implode(', ', array_merge(...array_values($array)));
echo "\n---\n";
echo array_reduce($array, function($carry, $item) {return $carry .= ($carry ? ', ' : '') . $item[0]; }, '');
Output:
Some message to display1, Some message to display2, Some message to display3
---
Some message to display1, Some message to display2, Some message to display3
The first technique will accommodate multiple elements in the subarrays, the second will not acknowledge more than one "message" per subarray.