You can have preg_split return the pieces it split on as well...
But here's an alternative:
$lines = file('pdu-assignments.txt');
$record_starts = array_keys(preg_grep('APC-ID-\d+', $lines));
$record_starts[] = count($lines);
$records = array();
for($i=0; $i < count($record_starts) - 1; $i++)
{
$records[] = array_slice($lines, $record_starts[$i], $record_starts[$i+1]-$record_starts[$i]);
}
(Note; untested). Next step would be to use the first element of each element of $records to obtain the key in a new array that would contain the rest of that $records element.
Actually, come to think of it, that could be done on $lines directly:
$current_key = null;
$apcs = array();
foreach($lines as $line)
{
if(preg_match('/^(\d+(?:\.\d){3}).*?APC-ID-(\d+)/', $line, $match))
{
$current_key = $match[2];
$apcs[$current_key]['IP'] = $match[1];
$apcs[$current_key]['ports'] = array();
}
else
{
// I'm just going to insert the raw line. Feel free to parse it first instead.
$apcs[$current_key]['ports'][] = $line;
}
}
(Still untested).