<?php
function parse(array $lines): array
{
$result = [];
foreach ($lines as $line) {
$split = explode(': ', $line, 2);
switch ($split[0]) {
case 'file':
unset($ref);
$ref = [null, null, null];
$result[] = &$ref;
break;
case 'Artist':
$ref[0] = $split[1];
break;
case 'Album':
$ref[1] = $split[1];
break;
case 'Title':
$ref[2] = $split[1];
break;
}
}
return $result;
}
$array = [
'MPD 0.23.5',
'file: Blondie/Parallel_Lines/12_Just_Go_Away.flac',
'Last-Modified: 2016-06-05T17:15:07Z',
'Format: 44100:16:2',
'Album: Parallel Lines',
'AlbumArtist: Blondie',
'Artist: Blondie',
'Composer: Debbie Harry',
'Date: 1978',
'Genre: Rock',
'Title: Just Go Away',
'Track: 12',
'Time: 214',
'duration: 213.800',
'Pos: 0',
'Id: 745',
'file: Blondie/Parallel_Lines/13_Once_I_Had_a_Love-aka_The_Disco_Song-1978_version-.flac',
'Last-Modified: 2016-06-05T17:15:01Z',
'Format: 44100:16:2',
'Album: Parallel Lines',
'AlbumArtist: Blondie',
'Artist: Blondie',
'Composer: Chris Stein; Debbie Harry',
'Date: 1978',
'Genre: Rock',
'Title: Once I Had a Love (aka The Disco Song) (1978 version)',
'Track: 13',
'Time: 198',
'duration: 198.000',
'Pos: 1',
'Id: 746',
'file: Blondie/Parallel_Lines/14_Bang_a_Gong-Get_It_On-live-.flac',
'Last-Modified: 2016-06-05T17:15:03Z',
'Format: 44100:16:2',
'Album: Parallel Lines',
'AlbumArtist: Blondie',
'Artist: Blondie',
'Composer: Marc Bolan',
'Date: 1978',
'Genre: Rock',
'Title: Bang a Gong (Get It On) (live)',
'Track: 14',
'Time: 330',
'duration: 330.293',
'Pos: 2',
'Id: 747',
''
];
var_export(parse($array));
preferences:
26.04 ms | 405 KiB | 5 Q