简体   繁体   English

比较和排序2d数组在php中的子值

[英]Compare and sort 2d array by its sub-value in php

I have seen so many duplicate questions for these.Really sorry for duplicate question.But not found the correct solution or may be not get correct logic. 我已经看到了这么多重复的问题。真的很抱歉重复的问题。但没有找到正确的解决方案或可能没有得到正确的逻辑。 I have already used usort() for these. 我已经使用了usort()来实现这些目的。 I have following array. 我有以下数组。

Array
(
    [0] => stdClass Object
        (
            [id] => 808
            [ScreenName] => Dispatch
            [OriginalName] => First Unit on Scene
            [OriginalPrintName] => First Unit on Scene
            [CurrentName] => Blank
        )

    [1] => stdClass Object
        (
            [id] => 809
            [ScreenName] => Dispatch
            [OriginalName] => EMS Agency
            [OriginalPrintName] => EMS Agency
            [CurrentName] => Blank
        )

    [2] => stdClass Object
        (
            [id] => 597
            [ScreenName] => Dispatch
            [OriginalName] => Date
            [OriginalPrintName] => Date
            [CurrentName] => Date 
        )
    [3] => stdClass Object
        (
            [id] => 509
            [ScreenName] => Dispatch
            [OriginalName] => Incident #
            [OriginalPrintName] => Incident #
            [CurrentName] => Incident #
        )
    [4] => stdClass Object
        (
            [id] => 758
            [ScreenName] => Dispatch
            [OriginalName] => Run# /Call #
            [OriginalPrintName] => Run#
            [CurrentName] => Run Call 
        )
)

By using following condition 通过使用以下条件

if (($y2->CurrentName == "") || ($y2->CurrentName == "Blank")) {
    print  $y2->OriginalName ;
} else {
    print  $y2->CurrentName ;
}

In list, printing all CurrentName but If currenName is blank then printing OriginalName 在列表中,打印所有CurrentName但如果currenName为空,则打印OriginalName
something like these. 像这样的东西。

Date
Incident #
Run Call
Type Of Service Req
Complaint Reported
Response Urgency
Location Type
Address Type
Address
Address Cont

I want to show these list albhabetically sorted. 我想显示这些列表按照白名单排序。 My usort() function is 我的usort()函数是

    usort($yvals, function ($elem1, $elem2) {
      if (($elem1->CurrentName == "" || $elem1->CurrentName == "Blank") && ($elem2->OriginalName == "" || $elem2->OriginalName == "Blank")) {
      return strcmp($elem1->OriginalName, $elem2->OriginalName);
  }else if(($elem1->CurrentName != "" || $elem1->CurrentName != "Blank") && ($elem2->CurrentName == "") || ($elem2->CurrentName == "Blank")){
       return strcmp($elem1->CurrentName, $elem2->OriginalName);
  }else{
     return strcmp($elem1->CurrentName, $elem2->CurrentName);
  }
});

Its working somewhere but not for all. 它在某个地方工作,但不适用于所有人。

尝试使用strcasecmp代替strcmp ......

This might be not optimal, but my experience with iterating over objects in php in some cases can produce counter-intuitive results, for the sake of simplicity I would separate sorting from filtering which you trying to achieve in one go. 这可能不是最佳的,但我在某些情况下迭代php中的对象的经验会产生反直觉的结果,为了简单起见,我会将排序与您想要一次性实现的过滤分开。 Also parenting your objects to a working class and moving the name retrieval to a method would be good programming practice. 同时将对象托管到工作类并将名称检索移动到方法也是很好的编程习惯。

class SomeWorkingClass{
    public function getProperName(){
    // proper name retrieval functionality
  }
}

usort($yvals, function ($elem1, $elem2) {
     return strcmp($elem1->getProperName(), $elem2->getProperName());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM