2013-06-24 27 views
0

我從Decode function down這個輸出數組:Print_r行錯誤? n嗎?

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

感謝您的幫助和長期問題抱歉..

+1

該工作已爲您完成: http://stackoverflow.com/questions/7025909/create-array-printed-with-print-r – immulatin

+0

https://gist.github.com/hakre/1102761 #file-printrparser-php [hakre](http://stackoverflow.com/users/367456/hakre)。 –

+0

如果包含所有相同的值,則不需要'$ replace'作爲數組。實際上你根本不需要'$ replace',因爲默認是一個空字符串。 – GolezTrol

回答

0

你可以使用正則表達式來掃描該鍵僅由空白組成:

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

foreach($blank_keys as $blank) { 
    unset($your_array[$blank]); 
} 
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:你永遠不能把足夠的意見在你的代碼,可能一開始似乎是浪費時間,但你或任何人誰​​擁有對你的代碼的工作,非常感謝有一天會...