如何自定义选择的数组顺序

时间:2016-08-24 17:17:38

标签: php mysql bug-tracking

我不是PHP开发人员(仅限Rails),但我在PHP代码(thebuggenie-app)中有任务告诉我必须更改显示选项的顺序。问题可能很简单,但对我来说不是那么容易。

我在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

现在我将开发人员作为第一个选项(默认),我需要将Tester作为第一个(默认)选项

如果代码可行,每个解决方案都适合我。)

更新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;
 }

1 个答案:

答案 0 :(得分: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";
}
?>