dongshi2458 2014-10-27 10:32
浏览 48
已采纳

array_merge在2个不同的实体集合上,按公共属性对它们进行排序

I have 2 entities, Invoice and Advance. Each have a "paidAt" property.

In a controller, I fetch both Invoice and Advance within a date range. Then, I merge them into 1 single array and push it into my view, where I display them in a list.

Before pushing them to my view, I would like to sort them using the "paidAt" property. I found some multi-dimensional sorts functions on PHP doc, but all make use of php native function array_multisort()

So Symfony throws me an error : Cannot use object of type Entity\Invoice as array

Is there anyway to do what I'm looking for ?

  • 写回答

2条回答 默认 最新

  • doujie4344 2014-10-27 13:44
    关注

    If you have getters getPaidAt() in both entities you can use for ascending order

    usort(
        $collection,
        function($a, $b) {
            if ($a->getPaidAt() === $b->getPaidAt()) {
                return 0;
            }
    
            return ($a->getPaidAt() < $b->getPaidAt())? -1 : 1;
        }
    );
    

    If you want to sort descending, just replace < by >.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大