print_r线虫? \ n吗?

时间:2013-06-24 19:10:04

标签: php arrays

我有Decode function down的这个OUTPUT数组:

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
        )
    [
] => ) 

所以,你可以看到:

    [
] => 

如何删除它?我尝试使用str_replace("\n", "", $arr);但这不起作用。 这是原始数组 - http://status.mc-host.cz/s8.mc-host.cz:25608-feed

这是我的功能代码:

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);

感谢您的帮助和对不起的长期问题...

2 个答案:

答案 0 :(得分:0)

您可以使用正则表达式扫描仅由空格组成的键:

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

foreach($blank_keys as $blank) {
   unset($your_array[$blank]);
}

答案 1 :(得分:0)

我会使用trim而不是str_replace。它更便宜,它可以处理尾随空间和任何空白区域。在您的情况下,您的函数可能看起来像这样:

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;
}

我没有测试代码,但我认为它应该可以正常工作。

PS:你永远不会在你的代码中添加足够的评论,一开始可能看起来浪费时间,但是你或者任何必须处理代码的人都会非常感激......