0

I have an array that contains player data. This array changes according to the number of players. The array looks like this:

Array
(
    [0] => Array
        (
            [Id] => 0
            [Name] => Playername1
            [Frags] => -3
            [Time] => 339
            [TimeF] => 05:39
        )

    [1] => Array
        (
            [Id] => 0
            [Name] => Playername2
            [Frags] => 0
            [Time] => 7
            [TimeF] => 00:07
        )

)

I want to get from this array from each player only the player name [name]. How do I do this? The output should be a string that looks something like this: Playername1, Playername2 I have not found anything on the Internet or on YouTube. The answer is certainly simple and obvious, but I have not found it.

Im using PHP 8.0.13.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
Niklas
  • 436
  • 1
  • 4
  • 16

2 Answers2

0

try something like this:

$player_names = '';
foreach($your_array as $key => $value){
    $player_names .= $value['Name'].', ';
    // this should concatenate all the player names
}
Farhan Ibn Wahid
  • 926
  • 1
  • 9
  • 22
0
$names = implode(',', array_column($arr, 'name'));
echo $names;
  • array_column: return the values from a single column in the input array.

  • implode: Join array elements with a string.

Faesal
  • 679
  • 6
  • 19