PHP数组不断添加单独的键值,而不是添加到现有键

时间:2012-09-27 22:02:24

标签: php arrays foreach

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

最后输入而不是计数器会额外增加。

我尝试了/ 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
*/

?>

2 个答案:

答案 0 :(得分:0)

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

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

解决方案是更好地消毒您的输入 - 看起来您应该在清理部分添加一些东西。 。 。也许是like this to strip UTF,或者如果这是你输入的最后一行,那里可能有一个EOF字符。 。 。

修改:根据您的评论,var_dump输出为:

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

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

    $all = array();
    foreach ($lines as $segment) {
        $all[] = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $segment);
    }

答案 1 :(得分:-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>