我正在尝试在 PHP 中进行和弦换位,弦值数组如下...
$chords1 = array('C','C#','D','D#','E','F','F#','G','G#','A','A#','B','C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','C');
一个例子是D6/F#。我想匹配数组值,然后将其转置为数组中给定的数字位置。这是我到目前为止...
function splitChord($chord){ // The chord comes into the function
preg_match_all("/C#|D#|F#|G#|A#|Db|Eb|Gb|Ab|Bb|C|D|E|F|G|A|B/", $chord, $notes); // match the item
$notes = $notes[0];
$newArray = array();
foreach($notes as $note){ // for each found item as a note
$note = switchNotes($note); // switch the not out
array_push($newArray, $note); // and push it into the new array
}
$chord = str_replace($notes, $newArray, $chord); // then string replace the chord with the new notes available
return($chord);
}
function switchNotes($note){
$chords1 = array('C','C#','D','D#','E','F','F#','G','G#','A','A#','B','C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','C');
$search = array_search($note, $chords1);////////////////Search the array position D=2 & F#=6
$note = $chords1[$search + 4];///////////////////////then make the new position add 4 = F# and A#
return($note);
}
这行得通,除了问题是,如果我使用像(D6/F#)这样的分裂和弦,则和弦被移调到 A#6/A#。它将第一个音符 (D) 替换为 (F#),然后将两个 (F#'s) 替换为 (A#)。
问题是......我怎样才能防止这种冗余发生。所需的输出将是F#6/A#。感谢您的帮助。如果解决方案已发布,我会将其标记为已回答。