0

I am creating a top-site and I have a question about echoing a certain element of a multidimensional-array. I am using a library called GameQ to ping gaming servers to grab statistics of said server. I am unable to print the results from certain elements in the array that I specify.

My Code:

        <?php

    // Include the main class file
    require '../GameQ.php';

    $gq = new GameQ();
    $gq->addServer(array(
        'id' => '0',
        'type' => 'minecraft',
        'host' => '192.210.238.76:25865',
    ));

    $results = $gq->requestData(); // Returns an array of results

//Below line returns a error: Notice: Undefined offset: 0
    echo "Online: " . $results[0]['numplayers'] . "/" . $results[0]['maxplayers'] . "<br />";

    var_dump($results);

    ?>

Array Var dump:

        array (size=1)
  '192.210.238.76:25865' => 
    array (size=17)
      'hostname' => string 'A Whole Other Level' (length=19)
      'gametype' => string 'SMP' (length=3)
      'game_id' => string 'MINECRAFT' (length=9)
      'version' => string '1.6.4' (length=5)
      'plugins' => string 'CraftBukkit on Bukkit 1.6.4-R0.1-SNAPSHOT' (length=41)
      'map' => string 'Build' (length=5)
      'numplayers' => string '6' (length=1)
      'maxplayers' => string '30' (length=2)
      'hostport' => string '25865' (length=5)
      'hostip' => string '192.210.238.76' (length=14)
      'players' => 
        array (size=6)
          0 => 
            array (size=1)
              ...
          1 => 
            array (size=1)
              ...
          2 => 
            array (size=1)
              ...
          3 => 
            array (size=1)
              ...
          4 => 
            array (size=1)
              ...
          5 => 
            array (size=1)
              ...
      'gq_online' => boolean true
      'gq_address' => string '192.210.238.76' (length=14)
      'gq_port' => string '25865' (length=5)
      'gq_protocol' => string 'gamespy3' (length=8)
      'gq_type' => string 'minecraft' (length=9)
      'gq_transport' => string 'udp' (length=3)

I have reviewed the manual for array's at Manual. I would appreciate anyone's help on this topic. I know I am missing something. I have tried to search for the answer first, before registering to ask this question. I would think searching for the results would be faster than waiting on someone to help me with the answer. Thank you for your patients and your understanding. Happy coding!

1 Answers1

0

The key 0 does not exist in the array,try with

$results['192.210.238.76:25865']['numplayers']

EDIT

As this is probably variable, you can fetch the first element of an array without modifying the array this way:

$firstValue = array_shift(array_values($results)); //the array which is the first value

$numplayers = $firstValue['numplayers'];

Line found in this post

Community
  • 1
  • 1
Johnride
  • 8,476
  • 5
  • 29
  • 39
  • Thank you! That fixed it right up. I tried adding the IP before, but forgot the ' and '. Thanks for helping me! – unenergizer Oct 10 '13 at 02:05