仅打印阵列中的特定内容和精美打印

时间:2017-12-22 10:16:20

标签: php arrays

我有一个以奇怪的方式打印的列表。我只想打印出名称,每个名字都在一个新行中。我怎么能这样做?

Player 1
Player 2
Player 3
Etc...

获取信息的PHP代码也有这个。不知道它做了什么,但仍然可能很重要。

    Header( 'Content-Type: text/plain' );
    Header( 'X-Content-Type-Options: nosniff' );

到目前为止我的输出看起来像这样。

Array
(
    [0] => Array
        (
            [Id] => 0
            [Name] => Player 1
            [Frags] => 0
            [Time] => 1779
            [TimeF] => 29:39
        )

    [1] => Array
        (
            [Id] => 0
            [Name] => Player 2
            [Frags] => 1
            [Time] => 1691
            [TimeF] => 28:11
        )

    [2] => Array
        (
            [Id] => 0
            [Name] => Player 3
            [Frags] => 0
            [Time] => 1662
            [TimeF] => 27:42
        )

    [3] => Array
        (
            [Id] => 0
            [Name] => Player 4
            [Frags] => 1
            [Time] => 1540
            [TimeF] => 25:40
        )

    [4] => Array
        (
            [Id] => 0
            [Name] => Player 5
            [Frags] => 0
            [Time] => 1495
            [TimeF] => 24:55
        )

    [5] => Array
        (
            [Id] => 0
            [Name] => Player 6
            [Frags] => 3
            [Time] => 1401
            [TimeF] => 23:21
        )

    [6] => Array
        (
            [Id] => 0
            [Name] => Player 7
            [Frags] => 0
            [Time] => 1280
            [TimeF] => 21:20
        )

    [7] => Array
        (
            [Id] => 0
            [Name] => Player 8
            [Frags] => 0
            [Time] => 1239
            [TimeF] => 20:39
        )

    [8] => Array
        (
            [Id] => 0
            [Name] => Player 9
            [Frags] => 1
            [Time] => 1126
            [TimeF] => 18:46
        )

    [9] => Array
        (
            [Id] => 0
            [Name] => Player 10
            [Frags] => 1
            [Time] => 492
            [TimeF] => 08:12
        )

    [10] => Array
        (
            [Id] => 0
            [Name] => Player 11
            [Frags] => 0
            [Time] => 272
            [TimeF] => 04:32
        )

)

我使用的PHP代码就是这个。

<?php
    require __DIR__ . '/../SourceQuery/bootstrap.php';

    use xPaw\SourceQuery\SourceQuery;

    // For the sake of this example
    Header( 'Content-Type: text/plain' );
    Header( 'X-Content-Type-Options: nosniff' );

    // Edit this ->
    define( 'SQ_SERVER_ADDR', '1111111111111' );
    define( 'SQ_SERVER_PORT', 11111111 );
    define( 'SQ_TIMEOUT',     1 );
    define( 'SQ_ENGINE',      SourceQuery::SOURCE );
    // Edit this <-

    $Query = new SourceQuery( );

    try
    {
        $Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );

print_r( $Query->GetPlayers( ) );


    }
    catch( Exception $e )
    {
        echo $e->getMessage( );
    }
    finally
    {
        $Query->Disconnect( );
    }

?>

4 个答案:

答案 0 :(得分:1)

怎么样:

<?php
foreach($input as $item) {
  echo $item['Name'];
}
?>

...给自己一个很好的PHP教程,而他们可能会在圣诞节期间打折;)

答案 1 :(得分:1)

<?php
    foreach ($arrayName as $key => $value) {
        echo $value['Name'] . '<br>';
    }

?>

答案 2 :(得分:1)

您可以使用PHP函数array_column仅从内部数组中提取玩家名称:array_column documentation

$playerNames = array_column($Query->GetPlayers( ), 'Name');

结果数组现在只包含可以打印出来的名称。

答案 3 :(得分:1)

其他人帮助了我,所以答案就是这样。 它只打印没有数组的名称。

代码。

foreach($Query->GetPlayers() as $player) {
  echo $player['Name'];
  echo "\n"; 
}

完整的代码。

<?php
    require __DIR__ . '/../SourceQuery/bootstrap.php';
 
    use xPaw\SourceQuery\SourceQuery;
   
    // For the sake of this example
    Header( 'Content-Type: text/plain' );
    Header( 'X-Content-Type-Options: nosniff' );
   
    // Edit this ->
    define( 'SQ_SERVER_ADDR', 'SERVER IP' );
    define( 'SQ_SERVER_PORT', PORT );
    define( 'SQ_TIMEOUT',     1 );
    define( 'SQ_ENGINE',      SourceQuery::SOURCE );
    // Edit this <-
   
    $Query = new SourceQuery( );
   
    try
    {
        $Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );
       
foreach($Query->GetPlayers() as $player) {
  echo $player['Name'];
  echo "\n"; 
}

 
    }
    catch( Exception $e )
    {
        echo $e->getMessage( );
    }
    finally
    {
        $Query->Disconnect( );
    }
   
?>