0

I have this OUTPUT array from Decode function down:

Array ( [
] => 
    [HostName] => Survival4fun
    [GameType] => SMP
    [Version] => 1.5.2
    [Plugins] => Array
        (
            [0] => WorldEdit
        )

    [Map] => world
    [Players] => 0
    [MaxPlayers] => 10
    [HostPort] => 25608
    [HostIp] => 31.133.13.99
    [RawPlugins] => WorldEdit5.5.6;
    [Software] => CraftBukkitonBukkit1.5.2-R0.1
    [Status] => online
    [Ping] => 15ms
    [
] => 
    [PlayersOnline] => Array
        (
            [P0] => NoPlayers
        )
    [
] => ) 

And so, you can see this:

    [
] => 

How can I remove it ? I tried using str_replace("\n", "", $arr); But this doesn't work. Here is the original array - http://status.mc-host.cz/s8.mc-host.cz:25608-feed

And here is my function code:

Function Decode_query($link) {
    $data = file($link, FILE_IGNORE_NEW_LINES);
    $arr = array();
    $string = array("[", "]", " ", "(", ")", "Array", "\n", "\r");
    $replace = array("", "", "", "", "", "", "", "");
    ForEach ($data as $line) {
        $s = str_replace($string, $replace, $line);
        If (Empty($s)) {} Else {
            $stat = explode("=>", $s);
            $P = str_replace("P", "", $stat[0]);
            If (is_numeric($stat[0])) {
                $arr["Plugins"][$stat[0]] = $stat[1];
            }
            ElseIf (is_numeric($P)) {
                $arr['PlayersOnline'][$stat[0]] = $stat[1];
            } Else {
                $arr[$stat[0]] = $stat[1];
            }
        }
    }
    Return $arr;
}
$arr = Decode_query("http://status.mc-host.cz/s8.mc-host.cz:25608-feed");
Print_r($arr);

Thanks for help and sorry for long question..

Filip Farkaš
  • 35
  • 1
  • 3
  • 1
    The work has already been done for you: http://stackoverflow.com/questions/7025909/create-array-printed-with-print-r – immulatin Jun 24 '13 at 19:16
  • https://gist.github.com/hakre/1102761#file-printrparser-php by [hakre](http://stackoverflow.com/users/367456/hakre). – Dave Chen Jun 24 '13 at 19:18
  • You don't need `$replace` to be an array if it contains all the same values. Actually you don't need `$replace` at all, since the default is an empty string. – GolezTrol Jun 24 '13 at 19:18
  • How are you downloading this file. I see a perfectly formatted associative array. Not like what you have posted. – immulatin Jun 24 '13 at 19:19
  • While PHP is case-insensitive, you should really not capitalize its syntax. – Jason McCreary Jun 24 '13 at 19:19

2 Answers2

0

You could use a regex to scan for keys that are composed of only whitespace:

$keys = array_keys($your_array);
$blank_keys = preg_grep('/^\s*$/', $keys);

foreach($blank_keys as $blank) {
   unset($your_array[$blank]);
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

I would work with trim in stead of str_replace. It is less expensive, and it takes care of the trailing spaces and whatever whitespace there may be. In your case your function would probably look something like this:

Function Decode_query($link) {
    // fetch the data
    $data = file($link, FILE_IGNORE_NEW_LINES);
    // prepare output array
    $arr = array('Plugins' => array(), 'PlayersOnline' => array());
    // prepare the list of characters we want to remove
    $removeChars = ' \t\n\r[]';

    ForEach ($data as $line) {
        // split line into key, value
        $stat = explode("=>", $line);
        // no 2 elements, means no '=>', so ignore line
        if (count($stat) <  2) continue; 
        // remove unwanted characters from key
        $trimmed = trim($stat[0], $removeChars);
        $pTrimmed = trim($trimmed, 'P');
        // if key = plugins, ignore line
        if ($trimmed == 'Plugins') continue; 
        // if key is numeric
        If (is_numeric($trimmed)) {
            // store in plugins subarray
            $arr['Plugins'][$trimmed] = trim($stat[1]);
        }
        // if (key - P) is numeric
        ElseIf (is_numeric($pTrimmed)) {
            // store in players online subarray
            $arr['PlayersOnline'][$pTrimmed] = trim($stat[1]);
        } Else {
            // all others store in level 1 array
            $arr[$trimmed] = trim($stat[1]);
        }
    }
    Return $arr;
}

I didn't test the code, but I think it should work fine.

PS: You can never put enough comments in your code, may seem a waste of time at first, but you, or anyone who has to work on your code, will be very grateful some day...

Pevara
  • 14,242
  • 1
  • 34
  • 47