Answers.Body: <p>I'd do it like this: </p>
  
  <ol>
  <li>First get the name of the select element, and then get the elemement using this </li>
  </ol>
  
  <p><code>$element =&amp; $form->getElement('name-of-select-element');</code></p>
  
  <p>i.e. don't hard code that 4.</p>
  
  <ol start="2">
  <li>You've got the options array correct, i.e. $element->_options</li>
  </ol>
  
  <p>Now you can just replace that options like you're doing, but depending on the widget in use it can be confusing how it's structured. The one I was working on recently has option['text'] as the label. Sorting by array values can be done manually or cleverly with some php and callback functions if you're ambitious.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-21T17:31:37.877Z
Answers.LastActivityDate: 2017-02-21T17:31:37.877Z
Answers.UserDisplayName: Alan Dixon
Body: <pre><code>function custom_changes_civicrm_buildForm($formName, &amp;$form) {
  if ($formName == 'CRM_Member_Form_Membership'){
    foreach($form-&gt;_elements as $key =&gt; $value){
      if ($key == 4){
      $assoc_array =  $value-&gt;_options[1][67];
        ksort($assoc_array,SORT_NUMERIC);
  unset($form-&gt;_elements[$key]-&gt;_options[1][67]);
       $form-&gt;_elements[$key]-&gt;_options[1][67] = $assoc_array;
      }
    }
  </code></pre>
  
  <p>I want to alter the options order by using ksort, but I dont find any function to alter the elements like setLabel which sets label</p>
  
  <p>Which is the function for altering and changing options in an element?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-02-18T17:49:09.203Z
Comments.Text: Why these options would be displayed in alpha order rather than in the order set in Membership Types is beyond me. I'd class that as a bug. What's the point in being able to order them if that order is not followed when displayed on a sign-up/renewal form?
Comments.UserDisplayName: Graham
Tags: <quickform>
Title: How to alter the options inside element of a civicrm new membership form?
ThreadText: # How to alter the options inside element of a civicrm new membership form?
  
  (Asked by: username_0 on 2017-02-18)
  
  function custom_changes_civicrm_buildForm($formName, &$form) {
  if ($formName == 'CRM_Member_Form_Membership'){
    foreach($form->_elements as $key => $value){
      if ($key == 4){
      $assoc_array =  $value->_options[1][67];
        ksort($assoc_array,SORT_NUMERIC);
  unset($form->_elements[$key]->_options[1][67]);
       $form->_elements[$key]->_options[1][67] = $assoc_array;
      }
    }
  
  I want to alter the options order by using ksort, but I dont find any function to alter the elements like setLabel which sets label
  Which is the function for altering and changing options in an element?
  
  **Tags:** quickform
  
  > **Comments:**
  > **username_1**: Why these options would be displayed in alpha order rather than in the order set in Membership Types is beyond me. I'd class that as a bug. What's the point in being able to order them if that order is not followed when displayed on a sign-up/renewal form?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2017-02-21. Score: 1
  
  I'd do it like this:
  
  1. First get the name of the select element, and then get the elemement using this
  
  `$element =& $form->getElement('name-of-select-element');`
  
  i.e. don't hard code that 4.
  
  2. You've got the options array correct, i.e. $element->\_options
  
  Now you can just replace that options like you're doing, but depending on the widget in use it can be confusing how it's structured. The one I was working on recently has option['text'] as the label. Sorting by array values can be done manually or cleverly with some php and callback functions if you're ambitious.
  
  
  ---
  
  