我如何排序在PHP以下数组/ stdClass的对象吗? [英] how do i sort the following array/stdclass object in php?

查看:225
本文介绍了我如何排序在PHP以下数组/ stdClass的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

做的是如何排序这个对象在PHP的POS'?

How do is sort this object by 'pos' in php?

Array ( 
[0] => stdClass Object ( [str] => Mondays [pos] => 170 ) 
[1] => stdClass Object ( [str] => Tuesdays [pos] => 299 )
[2] => stdClass Object ( [str] => Wednesdays [pos] => 355 )
[3] => stdClass Object ( [str] => Thursdays [pos] => 469 )
[4] => stdClass Object ( [str] => Fridays [pos] => 645 )
[5] => stdClass Object ( [str] => Mondays [pos] => 972 )
[6] => stdClass Object ( [str] => Tuesdays [pos] => 1033 ) 
[7] => stdClass Object ( [str] => Thursdays [pos] => 1080 )
[8] => stdClass Object ( [str] => Fridays [pos] => 1180 ) 

推荐答案

您也许可以使用 usort( )系列函数进行排序,要么在 STR POS 。你必须定义你自己的比较函数。

You could probably use the usort() family of functions to sort it either on str or pos. You have to define your own comparison function for that.

伪PHP例如:

function compareItems($a, $b)
{
    if ( $a->pos < $b->pos ) return -1;
    if ( $a->pos > $b->pos ) return 1;
    return 0; // equality
}

uasort($yourArray, "compareItems");

根据您的需求, 可能更适合其他的比较功能。

Depending on your needs, other comparison functions might be more appropriate.

这篇关于我如何排序在PHP以下数组/ stdClass的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆