我不是 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);
}
现在我将开发人员作为第一个选项(默认),并且我需要将测试人员作为第一个(默认)选项
如果代码可以工作,每个解决方案都适合我:)
更新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;
}
最佳答案
您可以使用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";
}
?>
关于php - 如何自定义选择的数组顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39129417/