2017-05-08 75 views
0

我加載此格式的蒼蠅多維數組:分割文件到PHP

///mvid:417815 qty:2 name:Aether Hub loc:Deck 
2 Aether Hub 
///mvid:423770 qty:2 name:Aetherstream Leopard loc:Deck 
2 Aetherstream Leopard 
///mvid:401837 qty:4 name:Canopy Vista loc:Deck 
4 Canopy Vista 
///mvid:426709 qty:2 name:Cartouche of Solidarity loc:Deck 
2 Cartouche of Solidarity

我希望做的是加載到一個數組,其中我有以下結構(我需要忽略每隔一行以及):

417815|2|Aether Hub|Deck 
423770|2|Aetherstream Leopard|Deck 
401837|4|Canopy Vista|Deck 
426709|2|Cartouche of Solidarity|Deck
+3

代碼在哪裏? –

+0

http://php.net/manual/en/function.file.php –

+0

你已經試過了什麼?做些努力,而不是僅僅在這裏拋出完整的問題/任務。 – jwenting

回答

0

這裏我們使用preg_split分裂正則表達式的基礎上

Try this code snippet here

<?php 
$string='///mvid:417815 qty:2 name:Aether Hub loc:Deck 
2 Aether Hub 
///mvid:423770 qty:2 name:Aetherstream Leopard loc:Deck 
2 Aetherstream Leopard 
///mvid:401837 qty:4 name:Canopy Vista loc:Deck 
4 Canopy Vista 
///mvid:426709 qty:2 name:Cartouche of Solidarity loc:Deck 
2 Cartouche of Solidarity'; 
$lines=explode("\n", $string);//optionally you can use file function to get the array of lines. 
$result=array(); 
for($x=0;$x<count($lines);$x+=2) 
{ 
    $split=preg_split("/[a-z]+:/",$lines[$x]); 
    unset($split[0]); 
    $result[]=implode("|",array_map("trim",array_values($split))); 
} 
print_r($result); 

輸出:

Array 
(
    [0] => 417815|2|Aether Hub|Deck 
    [1] => 423770|2|Aetherstream Leopard|Deck 
    [2] => 401837|4|Canopy Vista|Deck 
    [3] => 426709|2|Cartouche of Solidarity|Deck 
) 
1

我不知道你在哪裏得到那個格式加載,但如果你在一個變量都在這裏是你可以用它來實現想要你說的代碼。

<?php 
    $values = []; 
    $content = "///mvid:417815 qty:2 name:Aether Hub loc:Deck 
2 Aether Hub 
///mvid:423770 qty:2 name:Aetherstream Leopard loc:Deck 
2 Aetherstream Leopard 
///mvid:401837 qty:4 name:Canopy Vista loc:Deck 
4 Canopy Vista 
///mvid:426709 qty:2 name:Cartouche of Solidarity loc:Deck 
2 Cartouche of Solidarity"; 

    preg_match_all("#mvid:(\d+)\s+qty:(\d+)\s+name:([^:]+):(\w+)#", $content, $matches); 

    $count = count($matches[1]); // we store the count of the elements so that we don't call the function in every iteration of for loop 

    for($i = 0; $i < $count; $i++) { 
     $values[$i] = $matches[1][$i]."|".$matches[2][$i]."|".$matches[3][$i]."|".$matches[4][$i]; 
    } 

    echo "<pre>".print_r($values, true); 

輸出: - https://eval.in/789124

+0

不錯的答案。+1值得付出 –

0

感謝那些讓我我需要拆分的文件並加載件爲陣列的信息。從陣列中,我可以讓表單選擇適當的顯示卡。

我這是怎麼加載的文件:

$deck1 = []; 
$maincount = 0; 
$handle = @fopen("Deck1.dec", "r"); 
if ($handle) { 
    while (($buffer = fgets($handle, 4096)) !== false) { 
    preg_match_all("#mvid:(\d+)\s+qty:(\d+)\s+name:([^:]+):(\w+)#", $buffer, $matches); 
    $count = count($matches[1]); 
    for($i = 0; $i < $count; $i++) { 
     $deck1[$maincount][1] = $matches[1][$i]; 
     $deck1[$maincount][2] = $matches[2][$i]; 
     $deck1[$maincount][3] = rtrim($matches[3][$i], " loc"); 
     $deck1[$maincount][4] = $matches[4][$i]; 
    } 
    $maincount++; 
    } 
    //echo "<pre>".print_r($deck1, true); 
    if (!feof($handle)) { 
     echo "Error: unexpected fgets() fail\n"; 
    } 
    fclose($handle); 
} 

而且我這是怎麼創建單選按鈕。我需要在全球範圍內刪除某些卡片。

foreach ($deck1 as $index=>$option) { 
    if ($option[3] != "Plains" && $option[3] != "Forest" && $option[3] != "Mountain" && $option[3] != "Swamp" && $option[3] != "Island"){ 
     if ($option[4] == "SB"){ 
      echo "<i><input type=\"radio\" name='mText7bgo' value=\"{$option[1]}\">{$option[3]}</i><br>"; 
     } 
     else { 
      echo "<input type=\"radio\" name='mText7bgo' value=\"{$option[1]}\">{$option[3]}<br>"; 
     } 
    } 
} 

我確定有一些優化工作可以完成,但這樣做完成了工作。謝謝。