在php中使用usort对对象数组进行排序?
我确实查看了 usort,但仍然有点困惑...
这是 $myobject 对象的样子:
Array
(
[0] => stdClass Object
(
[tid] => 13
[vid] => 4
)
[1] => stdClass Object
(
[tid] => 10
[vid] => 4
)
[2] => stdClass Object
(
[tid] => 34
[vid] => 4
)
[3] => stdClass Object
(
[tid] => 9
[vid] => 4
)
我看到了这个:
function cmp( $a, $b )
{
if( $a->weight == $b->weight ){ return 0 ; }
return ($a->weight < $b->weight) ? -1 : 1;
}
usort($myobject,'cmp');
我正在尝试根据 tid 进行排序,但是,我想我只是不确定真的如果我必须改变重量?或者它会按原样工作吗?我尝试了一下,但没有任何输出...
I did look at usort, but am still a little confused...
Here is what the $myobject object looks like:
Array
(
[0] => stdClass Object
(
[tid] => 13
[vid] => 4
)
[1] => stdClass Object
(
[tid] => 10
[vid] => 4
)
[2] => stdClass Object
(
[tid] => 34
[vid] => 4
)
[3] => stdClass Object
(
[tid] => 9
[vid] => 4
)
I saw this:
function cmp( $a, $b )
{
if( $a->weight == $b->weight ){ return 0 ; }
return ($a->weight < $b->weight) ? -1 : 1;
}
usort($myobject,'cmp');
I'm trying to sort according to tid, but, I guess I'm just not sure really if I have to change weight to something? Or will it just work as is? I tried it, but nothing outputted...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
cmp 是一个回调函数,usort 使用它来比较复杂的对象(例如您的对象)以找出如何对它们进行排序。修改 cmp 供您使用(或将其重命名为您想要的任何名称)
http://www.php.net/usort
cmp is the a callback function that usort uses to compare complex objects (like yours) to figure out how to sort them. modify cmp for your use (or rename it to whatever you wish)
http://www.php.net/usort
我花了三个小时试图编写一个比较函数。事实上,这很容易,但我认为我遗漏了一些东西,并从头开始一次又一次地编写它,以多种方式用我的示例数组测试它的算法。
最后我意识到问题出在内部 uasort 函数上。它并没有完成与所有项目的比较。我现在不记得所用算法的名称,但我自己使用 C++ 的改进版本(我的)。该算法使用类似二叉树的比较方法,通过在每次使用新索引(下限、上限)递归调用排序函数时将数组划分为所需数量的对。
当剩余切片属于一项时,则上部索引和下部索引相同,并且函数认为它已经完成(处理了所有项目),尽管最后一项没有被评估。当最里面的块有奇数时,使用该算法的排序函数会失败。它可以很好地处理 2、4、8 .... 元素,但不能处理 3、5、7 等... 失败的确切条件取决于元素的排序顺序。数字可能并不总是有意义的。
我几年前就解决了这个问题。我现在对PHP无法自己解决,因为我没有PHP编译器,也没有PHP源代码。但是如果 PHP 开发团队的任何人联系我,我可以提供该算法的 C++ 工作副本。同样的算法是访问排序元素的最快方法。
I have been trying to write a comparing function for three hours. It is very easy in fact but I thought I was missing something and wrote it again and again from scratch changing algorithm in many ways testing it with my sample array.
At last I realized that the problem is at internal uasort function. It does not finish comparing with all items. I don't remember the name of used algorithm right now but I myself use an improved version (ow mine) in C++. The algorithm uses a binary-tree like comparison method by dividing the array into as many pairs as required in a recursive call to the sorting function with new indexes (lower, upper limits) each time.
When the remaining slice is of one item, then upper and lower indexes are the same and function thinks that it has finished (handled all items) although the last item was not evaluated. Sorting functions using that algorithm fail when the most-inner block has an odd number. It works fine 2, 4, 8 .... elements, but cant work with 3, 5, 7 etc... The exact condition of failure depends on the elements sort order. Numbers may not always be meaningful.
I solved that problem years ago. I cant solve it by myself for PHP now because I dont have a PHP compiler and I don't have PHP source code either. But if anybody from PHP development team contacts me, I can supply the working copy of that algorithm in C++. The same algorithm is the fastest way of accessing sorted elements.
对于获取属性 stdClass 对象,请使用运算符 ->{'name_property'},例如 $a->{'tid'}
For get property stdClass Object use operator ->{'name_property'}, eg $a->{'tid'}