I have a dynamic array, each group of lines start with a specific code "S10.G00.00", "S10.G00.01", "S10.G00.02" ...
The array may be like this:
array:20 [▼
0 => "S10.G00.00.001,'www.mywebsite.com'"
1 => "S10.G00.00.002,'Company name'"
2 => "S10.G00.00.003,'v2.01'"
3 => "S10.G00.01.001,'492484944'"
4 => "S10.G00.01.002,'00029'"
5 => "S10.G00.01.003,'First name'"
6 => "S10.G00.01.004,'120 R CHARLES'"
7 => "S10.G00.01.005,'92200'"
8 => "S10.G00.02.001,'01'"
9 => "S10.G00.02.002,'first name'"
10 => "S10.G00.02.004,'[email protected]'"
11 => "S10.G00.02.005,'0750000000'"
12 => "S20.G00.05.001,'01'"
13 => "S20.G00.05.002,'01'"
14 => "S20.G00.05.003,'11'"
15 => "S20.G00.05.004,'5'"
16 => "S20.G00.05.010,'01'"
17 => "S10.G00.01.001,'492484944'"
18 => "S10.G00.01.002,'00029'"
19 => "S10.G00.01.003,'Last name'"
]
I would like to select each group of successive lines that start with the same code (the first 10 characters) and put them into an associative array.
The associative array should be like this :
array:5 [▼
0 => array:1 [▼
"S10.G00.00" => array:3 [▼
0 => "S10.G00.00.001,'www.mywebsite.com'"
1 => "S10.G00.00.002,'Company name'"
2 => "S10.G00.00.003,'v2.01'"
]
]
1 => array:1 [▼
"S10.G00.01" => array:5 [▼
0 => "S10.G00.01.001,'492484944'"
1 => "S10.G00.01.002,'00029'"
2 => "S10.G00.01.003,'First name'"
3 => "S10.G00.01.004,'120 R CHARLES'"
4 => "S10.G00.01.005,'92200'"
]
]
2 => array:1 [▼
"S10.G00.02" => array:4 [▼
0 => "S10.G00.02.001,'01'"
1 => "S10.G00.02.002,'first name'"
2 => "S10.G00.02.004,'[email protected]'"
3 => "S10.G00.02.005,'0750000000'"
]
]
3 => array:1 [▼
"S10.G00.05" => array:5 [▼
0 => "S20.G00.05.001,'01'"
1 => "S20.G00.05.002,'01'"
2 => "S20.G00.05.003,'11'"
3 => "S20.G00.05.004,'5'"
4 => "S20.G00.05.010,'01'"
]
]
4 => array:1 [▼
"S10.G00.01" => array:3 [▼
0 => "S10.G00.01.001,'492484944'"
1 => "S10.G00.01.002,'00029'"
2 => "S10.G00.01.003,'Last name'"
]
]
]
All you need is a simple loop and to keep track of the previous 10 characters:
assert(array_is_list($arr));
$result = [];
$previousKey = null;
$index = 0;
foreach ($arr as $item)
{
$currentKey = substr($item, 0, 10);
$previousKey ??= $currentKey;
if ($previousKey !== $currentKey)
{
$index++;
$previousKey = $currentKey;
}
$result[$index][$currentKey][] = $item;
}