sig*_*nce 1 php arrays sorting object stdclass
如何在php中用'pos'对这个对象进行排序?
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 )
Run Code Online (Sandbox Code Playgroud)
)
你也许可以使用usort()的函数家族无论是在对其进行排序str或pos.你必须为此定义自己的比较函数.
伪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");
Run Code Online (Sandbox Code Playgroud)
根据您的需要,其他比较功能可能更合适.