php 数组对象排序,数组-PHP中的对象排序

数组-PHP中的对象排序

用PHP排序对象的一种优雅方法是什么? 我很乐意完成与此类似的事情。

$sortedObjectArary = sort($unsortedObjectArray, $Object->weight);

基本上指定要排序的数组以及要排序的字段。 我研究了多维数组排序,那里可能有一些有用的东西,但是我看不到任何优雅或明显的东西。

jW. asked 2020-08-02T07:59:28Z

10个解决方案

73 votes

手册几乎逐字记录:

function compare_weights($a, $b) {

if($a->weight == $b->weight) {

return 0;

}

return ($a->weight < $b->weight) ? -1 : 1;

}

usort($unsortedObjectArray, 'compare_weights');

如果希望对象能够自行排序,请参见此处的示例3:[http://php.net/usort]

Kent Fredric answered 2020-08-02T07:59:50Z

20 votes

对于php> = 5.3

function osort(&$array, $prop)

{

usort($array, function($a, $b) use ($prop) {

return $a->$prop > $b->$prop ? 1 : -1;

});

}

请注意,这使用了匿名函数/闭包。 可能会发现复习有关此文档的php文档很有用。

Will Shaver answered 2020-08-02T08:00:15Z

5 votes

如果您想要那种级别的控制,甚至可以将排序行为构建到要排序的类中

class thingy

{

public $prop1;

public $prop2;

static $sortKey;

public function __construct( $prop1, $prop2 )

{

$this->prop1 = $prop1;

$this->prop2 = $prop2;

}

public static function sorter( $a, $b )

{

return strcasecmp( $a->{self::$sortKey}, $b->{self::$sortKey} );

}

public static function sortByProp( &$collection, $prop )

{

self::$sortKey = $prop;

usort( $collection, array( __CLASS__, 'sorter' ) );

}

}

$thingies = array(

new thingy( 'red', 'blue' )

, new thingy( 'apple', 'orange' )

, new thingy( 'black', 'white' )

, new thingy( 'democrat', 'republican' )

);

print_r( $thingies );

thingy::sortByProp( $thingies, 'prop1' );

print_r( $thingies );

thingy::sortByProp( $thingies, 'prop2' );

print_r( $thingies );

Peter Bailey answered 2020-08-02T08:00:36Z

4 votes

对于该比较功能,您可以执行以下操作:

function cmp( $a, $b )

{

return $b->weight - $a->weight;

}

Tom answered 2020-08-02T08:00:56Z

2 votes

usort函数([http://uk.php.net/manual/en/function.usort.php)]是您的朋友。 就像是...

function objectWeightSort($lhs, $rhs)

{

if ($lhs->weight == $rhs->weight)

return 0;

if ($lhs->weight > $rhs->weight)

return 1;

return -1;

}

usort($unsortedObjectArray, "objectWeightSort");

请注意,所有阵列键都会丢失。

Adam Wright answered 2020-08-02T08:01:20Z

1 votes

您可以使用usort()函数并创建自己的比较函数。

$sortedObjectArray = usort($unsortedObjectArray, 'sort_by_weight');

function sort_by_weight($a, $b) {

if ($a->weight == $b->weight) {

return 0;

} else if ($a->weight < $b->weight) {

return -1;

} else {

return 1;

}

}

Jeremy Ruten answered 2020-08-02T08:01:40Z

0 votes

根据您要解决的问题,您可能还会发现SPL接口有用。 例如,实现ArrayAccess接口将使您可以像访问数组一样访问类。 同样,实现SeekableIterator接口可以让您像数组一样遍历对象。 这样,您可以对对象进行排序,就好像它是一个简单的数组一样,可以完全控制给定键返回的值。

更多细节:

Zend文章

PHPriot文章

PHP手册

Wilco answered 2020-08-02T08:02:18Z

0 votes

function PHPArrayObjectSorter($array,$sortBy,$direction='asc')

{

$sortedArray=array();

$tmpArray=array();

foreach($this->$array as $obj)

{

$tmpArray[]=$obj->$sortBy;

}

if($direction=='asc'){

asort($tmpArray);

}else{

arsort($tmpArray);

}

foreach($tmpArray as $k=>$tmp){

$sortedArray[]=$array[$k];

}

return $sortedArray;

}

例如=>

$myAscSortedArrayObject=PHPArrayObjectSorter($unsortedarray,$totalMarks,'asc');

$myDescSortedArrayObject=PHPArrayObjectSorter($unsortedarray,$totalMarks,'desc');

biswarupadhikari answered 2020-08-02T08:02:38Z

0 votes

您可以使用与Nspl中的排序函数发布的代码几乎相同的代码:

use function \nspl\a\sorted;

use function \nspl\op\propertyGetter;

use function \nspl\op\methodCaller;

// Sort by property value

$sortedByWeight = sorted($objects, propertyGetter('weight'));

// Or sort by result of method call

$sortedByWeight = sorted($objects, methodCaller('getWeight'));

Ihor Burlachenko answered 2020-08-02T08:02:58Z

-3 votes

如果要在PHP中探索lambda样式函数的全部(令人恐惧的)范围,请参阅:[http://docs.php.net/manual/en/function.create-function.php]

Internet Friend answered 2020-08-02T08:03:18Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值