我正在尝试订购我的阵列。我的第一个数组将具有值字符串值,其中很少有相同的值现在我想知道每个值有多少,并将其添加到新数组中作为键和值它现在出现在第一个数组中的次数我得到这样做,但我得到了一个错误。
最后一个条目而不是被反击会被额外添加。
我尝试了 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
*/
?>