2016-08-24 121 views
0

我不是PHP開發人員(僅限Rails),但我有PHP代碼(thebuggenie-app)中的任務,它告訴我必須更改顯示select選項的順序。問題可能很簡單,但對我來說並不那麼容易。如何自定義陣列順序

我在PHP項目都看到我有這樣的事情,選擇(後小調查):

<optgroup label="<?php echo __('Globally available roles'); ?>"> 
<?php foreach ($global_roles as $role): ?> 
    <option value="<?php echo $role->getId(); ?>"> 
    <?php echo $role>getName(); ?></option> 
<?php endforeach ;?> 
</optgroup> 

和(可能)global_roles是將迭代一個數組。在Rails中很容易定製訂單,但這種情況我不知道。如果您需要任何信息,請告訴我。

在動作類,我發現:$this->global_roles = TBGRole::getAll();

這一個重定向我的腦海裏:

/** 
* Returns all project roles available 
* 
* @return array 
*/  
public static function getAll() 
{ 
    return TBGListTypesTable::getTable()->getAllByItemTypeAndItemdata(self::ROLE, null); 
} 

enter image description here

現在我有開發商作爲第一選項(默認),我需要將測試儀作爲第一個(默認)選項

如果代碼可以工作,那麼每個解決方案都適合我:)

更新1)

public function getAllByItemTypeAndItemdata($itemtype, $itemdata) 
{ 
    $this->_populateItemCache(); 
    $items = (array_key_exists($itemtype, self::$_item_cache)) ? self::$_item_cache[$itemtype] : array(); 
    foreach ($items as $id => $item) 
    { 
     if ($item->getItemdata() != $itemdata) unset($items[$id]); 
    } 

    return $items; 
} 
+0

這是更多的SQL問題。你能提供'getAllByItemTypeAndItemdata()'代碼嗎? – jaro1989

+0

getAllByItemTypeAndItemdata()in update 1) – Skrypter

回答

2

您可以使用usort()。 此函數使用第二個函數進行排序,您創建並傳遞參數。 例如:

<?php 
function cmp($a, $b) 
{ 
    if ($a == $b) { 
    return 0; 
    } 
    return ($a < $b) ? -1 : 1; 
} 

$a = array(3, 2, 5, 6, 1); 

usort($a, "cmp"); 

foreach ($a as $key => $value) { 
    echo "$chave: $valor\n"; 
} 
?>