I'm trying to order my array. My first array will have values string values few of them will be same now i want to know how many of each value is there and add that to a new array as key and value the number of how many times it appears in first array now i get to do this yet i get a bug.
Last entry instead of being counter in gets added extra.
I tried while/for/foereach same result.
Code:
<?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
*/
?>
 
     
     
    