Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8686863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:58:19+00:00 2026-06-12T22:58:19+00:00

I am trying to do chord transposition in PHP the array of Chord values

  • 0

I am trying to do chord transposition in PHP the array of Chord values are as followed…

$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');

An example would be D6/F#. I want to match the array value and then transpose it by a given number position in the array. Here is what I have so far…

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);
}

This works, except the problem is that if I use a split chord like (D6/F#) The chord is transposed to A#6/A#. It is replacing the first note (D) with an (F#) then, Both (F#’s) with an (A#).

The question is… How can I keep this redundancy from happening. The desired output would be F#6/A#. Thank you for your help. If the solution is posted, I WILL mark it as answered.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:58:20+00:00Added an answer on June 12, 2026 at 10:58 pm

    You can use preg_replace_callback function

    function transposeNoteCallback($match) {
        $chords = 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');
        $pos = array_search($match[0], $chords) + 4;
        if ($pos >= count($chords)) {
            $pos = $pos - count($chords);
        }
        return $chords[$pos];
    }
    
    function transposeNote($noteStr) {
        return preg_replace_callback("/C#|D#|F#|G#|A#|Db|Eb|Gb|Ab|Bb|C|D|E|F|G|A|B/", 'transposeNoteCallback', $noteStr);
    }
    

    Test

    echo transposeNote(“Eb6 Bb B Ab D6/F#”);

    returns

    G6 C# Eb C F#6/A#

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a Chord distributed hash table. I want to use SHA-1
What i'm trying to achieve is playing a guitar chord from my python application.
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
I am trying to filter a list of Chord objects (class) in C# using
I'm trying to develop an app that gets four coord. values from an XML
I'm trying to rewrite a function in javascript to php but it is not
I'm trying to organize the data in the file.dat, such that I could then
I want to use an array to play music/sounds. The output music/sounds needs to
I am trying to call google maps geocode and am following the example on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.