Home > Software design >  How to get data from big Array in PHP
How to get data from big Array in PHP

Time:12-13

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.

CodePudding user response:

I have assigned the player array to the variable $array2. In this case it works with:

$players = array_column($array2, 'Name');
echo implode(", ",$players);

CodePudding user response:

try something like this:

$player_names = '';
foreach($your_array as $key => $value){
    $player_names .= $value['Name'].', ';
    // this should concatenate all the player names
}

CodePudding user response:

$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.

  • Related