Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 迭代按某个字段排序的原则集合_Php_Sorting_Collections_Doctrine_Doctrine 1.2 - Fatal编程技术网

Php 迭代按某个字段排序的原则集合

Php 迭代按某个字段排序的原则集合,php,sorting,collections,doctrine,doctrine-1.2,Php,Sorting,Collections,Doctrine,Doctrine 1.2,我需要这样的东西: $products = Products::getTable()->find(274); foreach ($products->Categories->orderBy('title') as $category) { echo "{$category->title}<br />"; } class ExampleClass { public

我需要这样的东西:

        $products = Products::getTable()->find(274);
        foreach ($products->Categories->orderBy('title') as $category)
        {
            echo "{$category->title}<br />";
        }
class ExampleClass
{

    public static function sortByAge( $a , $b )
    {
         $age_a = $a->age;
         $age_b = $b->age;

         return $age_a == $age_b ? 0 : $age_a > $age_b ? 1 : - 1;
    }    

    public function sortExample()
    {
         $users = User::getTable()->findAll();
         $users ->sortBy('ExampleClass::sortByAge');

         echo "Oldest User:";
         var_dump ( $users->end() );
    }

}
$products=products::getTable()->find(274);
foreach($products->CATEGORES->orderBy('title')作为$category)
{
回声“{$category->title}
”; }
我知道这是不可能的,但是。。。我如何在不创建条令查询的情况下执行类似操作


谢谢。

我刚才也在看同样的问题。您需要将集合转换为数组:

$someDbObject = Doctrine_Query::create()...;
$children = $someDbObject->Children;
$children = $children->getData(); // convert from Doctrine_Collection to array
然后,您可以创建自定义排序函数并调用它:

// sort children
usort($children, array(__CLASS__, 'compareChildren')); // fixed __CLASS__
其中compareChildren看起来像:

private static function compareChildren($a, $b) {
   // in this case "label" is the name of the database column
   return strcmp($a->label, $b->label);
}
您还可以执行以下操作:

$this->hasMany('Category as Categories', array(...
             'orderBy' => 'title ASC'));
在架构文件中,它看起来像:

  Relations:
    Categories:
      class: Category
      ....
      orderBy: title ASC

您可以向Colletion.php添加排序函数:

public function sortBy( $sortFunction )
{
    usort($this->data, $sortFunction);
}  
按年龄对用户集合进行排序如下所示:

        $products = Products::getTable()->find(274);
        foreach ($products->Categories->orderBy('title') as $category)
        {
            echo "{$category->title}<br />";
        }
class ExampleClass
{

    public static function sortByAge( $a , $b )
    {
         $age_a = $a->age;
         $age_b = $b->age;

         return $age_a == $age_b ? 0 : $age_a > $age_b ? 1 : - 1;
    }    

    public function sortExample()
    {
         $users = User::getTable()->findAll();
         $users ->sortBy('ExampleClass::sortByAge');

         echo "Oldest User:";
         var_dump ( $users->end() );
    }

}

您可以使用集合迭代器:

$collection=Table::getInstance()->findAll();
$iter=$collection->getIterator();
$iter->uasort(函数($a,$b){
$name_a=(int)$a->getName();
$name_b=(int)$b->getName();
返回$name\u a==$name\u b?0:$name\u a>$name\u b?1:-1;
});        
foreach($iter作为$element){
//…现在您可以迭代已排序的集合
}
如果要使用u_toString方法对集合进行排序,则会更容易:

foreach($collection->getIterator()->asort()作为$element){/*…*/}

如果排序是“永久性”的,那么使用这种方法比使用Chris William的方法要好得多。缺点是永久性。向此关系的每个查询中添加orderBy将影响性能。或者通过
@orderBy
注释:您的解决方案只有在我将其更改为:usort($children,array(u__________________;尝试使用
$collection->getIterator()->asort()
,但它只返回bool。对不起,我忘了它是如何工作的。你说得对,成功时返回true,失败时返回false。这是一个很好的设计。如果在迭代之前调用asort,您将能够遍历排序列表。