2017-05-08 78 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>"; 
     } 
    } 
} 

我确定有一些优化工作可以完成,但这样做完成了工作。谢谢。