-2

我正在尝试订购我的阵列。我的第一个数组将具有值字符串值,其中很少有相同的值现在我想知道每个值有多少,并将其添加到新数组中作为键和值它现在出现在第一个数组中的次数我得到这样做,但我得到了一个错误。

最后一个条目而不是被反击会被额外添加。

我尝试了 while/for/foereach 相同的结果。

代码:

<?php
 function add_fit($fitting)
   {
        // Save raw fit
        $eft = $fitting;

        // Clean for import
        $fit = trim($fitting);
        $lines = explode("\n", $fit);
        $lines = array_filter($lines, 'trim');

        // Split first line for name and type
        $details = str_replace('[', '', $lines[0]);
        $details = str_replace(']', '', $details);
        $split_details = explode(', ', $details);

        $ship = $split_details[0];
        $name = $split_details[1];

        foreach ($lines as $line => $module) {
            if(strstr($module, '[empty '))
            {
                unset($lines[$line]);
            }
        }

        $all = array();
        foreach ($lines as $index => $segment) {
            array_push($all, $segment);
        }

        $modules = array();
        for($i = 1; $i < count($all); $i++)
        {
            if(isset($modules[$all[$i]]))
            {
                $modules[$all[$i]] = $modules[$all[$i]]+1;
            } else {
                $modules[$all[$i]] = 1;
            }
        }

        var_dump($modules);

   }

/*
The $fitting is as follows:

[Thrasher, Buffer Thrasher]
Gyrostabilizer II
Gyrostabilizer II

Small F-S9 Regolith Shield Induction
Small F-S9 Regolith Shield Induction
1MN MicroWarpdrive I

280mm Howitzer Artillery II
280mm Howitzer Artillery II
280mm Howitzer Artillery II
280mm Howitzer Artillery II
280mm Howitzer Artillery II
280mm Howitzer Artillery II
280mm Howitzer Artillery II
[empty high slot]

Small Ancillary Current Router I
Small Ancillary Current Router I
Small Ancillary Current Router I


And the method returns:
  'Gyrostabilizer II'                    => int 2
  'Small F-S9 Regolith Shield Induction' => int 2
  '1MN MicroWarpdrive I'                 => int 1
  '280mm Howitzer Artillery II'          => int 7
  'Small Ancillary Current Router I'     => int 2
  'Small Ancillary Current Router I'     => int 1


While it should return:
  'Gyrostabilizer II'                    => int 2
  'Small F-S9 Regolith Shield Induction' => int 2
  '1MN MicroWarpdrive I'                 => int 1
  '280mm Howitzer Artillery II'          => int 7
  'Small Ancillary Current Router I'     => int 3   <-- Like this
*/

?>
4

2 回答 2

0

您的输入中必须有一些非打印字符,因为哈希不能具有具有多个值的相同键(这是您的输出显示的 - 的两个键Small Ancillary Current Router I,它们必须不同,因为它们具有不同的值......)

什么是var_dump(array_keys($modules))节目?

解决方案是更好地清理您的输入 - 看起来您可能应该在您的清理部分添加一些内容。. . 也许像这样剥离 UTF,或者如果这是您输入的最后一行,则可能有一个 EOF 字符。. .

编辑:根据您的评论,var_dump输出为:

5 => string 'Small Ancillary Current Router I' (length=33) 
6 => string 'Small Ancillary Current Router I' (length=32)

请注意,一个是 33,另一个是 32,这意味着非打印字符会导致差异。此处描述了一种删除所有非打印字符的霰弹枪方法。要在您的代码中实现:

    $all = array();
    foreach ($lines as $segment) {
        $all[] = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $segment);
    }
于 2012-09-27T22:36:38.430 回答
-1

您可能应该使用array_key_exists()而不是 isset。原因可以在this SO question中看到

无论哪种方式,您都有一些空格导致问题。尝试在以下行中添加修剪,它应该可以工作:

foreach ($lines as $index => $segment) { 
    array_push($all, $segment); 
} 

改成:

foreach ($lines as $index => $segment) { 
    array_push($all, trim($segment)); 
} 

顺便说一句:o7 安全飞行 ;-)

更新:继承人我用于测试的代码:

<!DOCTYPE html>
<html>

<head>
</head>
<body>
<?php 

$whatever = "[Thrasher, Buffer Thrasher] 
Gyrostabilizer II 
Gyrostabilizer II 

Small F-S9 Regolith Shield Induction 
Small F-S9 Regolith Shield Induction 
1MN MicroWarpdrive I 

280mm Howitzer Artillery II 
280mm Howitzer Artillery II 
280mm Howitzer Artillery II 
280mm Howitzer Artillery II 
280mm Howitzer Artillery II 
280mm Howitzer Artillery II 
280mm Howitzer Artillery II 
[empty high slot] 

Small Ancillary Current Router I 
Small Ancillary Current Router I 
Small Ancillary Current Router I";

echo '<pre>';
print_r(add_fit($whatever));
echo '</pre>';


 function add_fit($fitting) 
   { 
        // Save raw fit 
        $eft = $fitting; 

        // Clean for import 
        $fit = trim($fitting); 
        $lines = explode("\n", $fit); 
        $lines = array_filter($lines, 'trim'); 

        // Split first line for name and type 
        $details = str_replace('[', '', $lines[0]); 
        $details = str_replace(']', '', $details); 
        $split_details = explode(', ', $details); 

        $ship = $split_details[0]; 
        $name = $split_details[1]; 

        foreach ($lines as $line => $module) { 
            if(strstr($module, '[empty ')) 
            { 
                unset($lines[$line]); 
            } 
        } 

        $all = array(); 
        foreach ($lines as $index => $segment) { 
            array_push($all, trim($segment)); 
        } 

        $modules = array(); 
        for($i = 1; $i < count($all); $i++) 
        { 
            if(isset($modules[$all[$i]])) 
            { 
                $modules[$all[$i]] = $modules[$all[$i]]+1; 
            } else { 
                $modules[$all[$i]] = 1; 
            } 
        } 

        return $modules; 

   } 

?>
</body>
</html>
于 2012-09-27T22:29:06.137 回答