2016-08-24 116 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"; 
} 
?>