通过具有" /"的日期对多个数组进行排序

时间:2018-05-02 14:52:51

标签: php arrays sorting date

我一直在尝试各种各样但却无法做到这一点。我需要通过下面JSON中的OpenDate字段对JSON数组进行排序。在这方面,我已经在Stack Overflow上尝试了大多数示例。

我将数组转换为更简单的数组,即

if (isset($array[2])) {
    $objJobs = array();
    foreach($array[2] as $row) {
           $objJobs[] = $row;
    }
}

然后对其进行排序:

usort($objJobs, function ($a, $b) {
        return strtotime($a['OpenDate']) - strtotime($b['OpenDate']);
    });

并尝试了

function doSortBy($a, $b)
{
    $t1 = date('Y-m-d',strtotime($a['OpenDate']));
    echo $t1;
    $t2 = date('Y-m-d',strtotime($b['OpenDate']));
    return $t2 - $t1;  // descending
}

以及其他各种但无法通过OpenDate

进行排序

这是JSON中的数组

 [
"Job",
"",
[
{
AppDataId: 144150,
JobTitle: "Network Maintenance Manager",
DescriptionOfRole: "Planning and monitoring the main operations within the organisation Benchmarking quality, time and productivity against best/safe practices Designing company equipment and management of policies Developing management processes and maintenance programmes for the operation and maintenance of a power distribution network Responsible for overall project performance of a project across multiple functions Identifying and approaching new client groups and involving them with the projects and solutions Developing and management a budget to ensure actioning of the goals & interpreting financial statements. Understanding the industry and market-share environment while using this information to improve the company’s results Working knowledge of simulation tools, power system characteristics and identifying possible problems which may arise Co ordinating and commissioning the sub stations and overall quality control A specialist understanding of the maintenance execution process, procedures and standards Actively seeking opportunities to attract internal and external talent to build relationships over the long term. stay abreast with economic, business, social, political, and technological trends In-depth knowledge and systemic understanding of relevant laws impacting the Business Unit Coordinating the commissioning of multiple sub-stations, formulating policy, setting standards, devising build processes and overall quality control. Expert knowledge of both statutory and operational requirements as applied to the field of operation Ability to manage most facets pertaining to health, safety and the environment. Ability to negotiate or resolve conflict and differences, agree upon courses of action, bargain for advantage, to satisfy various needs A specialist understanding of the maintenance execution process, policy, procedures and standards.",
Overview: "Company: Well Established Generation Company Location: Gaborone Date Posted: 02 May 2018 Industry: Power Generation Nationality: Open Salary: Negotiable Depending on Experience + Benefits Qualifications: Electrical Engineering Degree Assignment: Permanent",
MinimumRequirements: "Electrical Engineering Degree from a recognised institution Professionally registered engineering membership & additional Management Around 7 years + experience after qualification obtained within the power industry of which 3 + years must have been at a management level",
AssignedToName: "Yvette Rautenbach",
EmailJobApplicationTo: "jobs@adogreen.com",
Salary: "Negotiable Depending on Experience + Benefits ",
Status: "open",
StatusDescription: "",
Published: "yes",
Keywords: "Network, Maintenance, Manager",
ContractType: "Permanent",
Sector: "Power Generation",
PnetSector: "Electricity, Energy & Water Supply",
Area: "Southern Africa",
Country: "Botswana",
Province: "",
Location: "Gaborone",
Description: "P60K – P65K TCTC Open Negotiable and flexible. – tax bracket differences 25% tax – R75K – R90K CTC per month plus housing and bonus (Dep on exp) Housing given MA, pension/gratuity - of basic plus car allowance 25 of that collectively Cell phone included in there 13th Cheque no – only performance bonus – 30% of one annual basic salary ",
ShortDescription: "In reporting to the General Manager, the successful candidate will be responsible to control, develop and implement best practices and standards. Ensuring the operations and maintenance of the transmissions and distribution lines while managing risks associated with the projects and company.",
OpenDate: "02/04/2018",
StartDate: "",
ImageUploadedId: "",
CloseDate: "",
Abstract: "Network Maintenance Manager - open",
TimeStamp: "2018-05-02 16:17:31+02"
},
{
AppDataId: 144129,
JobTitle: "Network Development Manager",
DescriptionOfRole: "Managing and controlling the provision of project management and power system planning Planning of the Transmissions & distribution network development plans within the organisation Developing network planning Ensuring proficient time & productivity is spend on best practices Ensuring safe working environments and operations Coordinate and implement projects and programmes Implementing new business models and structures in line with the technology advancements Developing and managing the department budget and analysing the financial reports Working knowledge of Network Planning, doing contract negotiations and management Leading managers in terms of accountability and competitiveness Use networking opportunities, consulting and contributing to others’ decision-making processes Identify and assess risks in terms of statistics, severity and consequences of impact. The ability to stay abreast with research to stay current with best practices. Pro-actively creating learning opportunities through knowledge-sharing, experimentation & exploration Development and mentorship for junior staff and middle management Benchmarking and identifying relationships that are relevant to achieving business goals. Understanding the industry, competitors, economy and market related shares. Ensure project finance in addition to larger scale projects Keeping abreast of legislation & adapting Business Unit to legislation. Advanced Knowledge of: The Network Planning and Eskom investment principle. Integration of sourced data and trends into basic position proposals or recommendations with focused communication in order to achieve strategic direction.",
Overview: "Company: Well Established Generation Company Location: Gaborone Date Posted: 02 May 2018 Industry: Power Generation Nationality: Open Salary: Negotiable Depending on Experience + Benefits Qualifications: Electrical Engineering Degree Assignment: Permanent",
MinimumRequirements: "Electrical Engineering Degree from a recognized institution Professionally registered engineering membership & additional Management & HR Sciences qualification advantageous Around 7 years + experience after qualification obtained within the power industry of which 3 + years must have been at a management level",
AssignedToName: "Yvette Rautenbach",
EmailJobApplicationTo: "jobs@adogreen.com",
Salary: "Negotiable Depending on Experience + Benefits ",
Status: "open",
StatusDescription: "Network Development Manager",
Published: "yes",
Keywords: "Network, Manager",
ContractType: "Permanent",
Sector: "Power Generation",
PnetSector: "Automotive Sector",
Area: "Southern Africa",
Country: "Botswana",
Province: "",
Location: "Gaborone",
Description: "P60K – P65K TCTC Open Negotiable and flexible. – tax bracket differences 25% tax – R75K – R90K CTC per month plus housing and bonus (Dep on exp) Housing given MA, pension/gratuity - of basic plus car allowance 25 of that collectively Cell phone included in there 13th Cheque no – only performance bonus – 30% of one annual basic salary ",
ShortDescription: "In reporting to the General Manager, the successful candidate will be responsible for the planning of distributions and transmission of the distribution network plans within the organisation enduring proficient time is spend on the projects and a good outset of productivity.",
OpenDate: "02/05/2018",
StartDate: "",
ImageUploadedId: "",
CloseDate: "",
Abstract: "Network Development Manager - open",
TimeStamp: "2018-05-02 15:58:56+02"
},
{
AppDataId: 144087,
JobTitle: "Customer Services & Supply Manager",
DescriptionOfRole: "Higher level strategic (35-50 level) contacts with client/staff with regards to the processes and products innovation to ensure improvement on all service levels. Providing quality customer service whilst ensuring an increase in customer base and sales Managing the risks of unplanned outages Customer focus- Focusing on the current, future and internal/ external clients & planning and coordination while managing the activities of customer services and supplies Execution & delivery- Planning of monthly tactical planning and monitoring within the organisation. Financial Management - Developing and managing the budget and ensuring control costing and adhering to the budget Distribution system operations and maintenance- as a qualified engineer you will develop management processes and maintenance systems Networking & partnering- Building networks across functions Technical Expertise and knowledge- Processing general knowledge over the discipline and some in depth specialization of the field Coaching and development-Pro-actively creating study/learning opportunities within a shared knowledge exploration. Aligning development plans and actioning thereof to aspire employees, Leading the change- Leading initiatives within a function level & leading of other managers within the company Developing Junior & middle management talents Communication through project platforms and presentations or meetings Staying abreast with the economic, business, social/political & technological trends Measure of customer satisfaction- Applying service tools to measure the degree of customers and their satisfaction with services/ products provided Understanding the best equipment and suppliers to use on a specific project Network planning and customer technical support- Dealing with complex customer and managing customer relations Expert knowledge of both statutory/operational requirements as applied to the field of every day operations and ability to manage or action most aspects regarding health, safety and the surrounding environment. Project management- Responsible for overall project performance and functions Negotiation and conflict resolution- Dealing with conflict and resolution thereof. Integration of sourced data and trends ",
Overview: "Company: Well Established Generation Company Location: Gaborone Date Posted: 02 May 2018 Industry: Power Generation Nationality: Open Salary: Negotiable Depending on Experience + Benefits Qualifications: Electrical Engineering Degree with a relevant tertiary qualification in Business Admin Assignment: Permanent",
MinimumRequirements: "Electrical Engineering Degree with a relevant tertiary qualification in Business Administration Around 7 years + experience after qualification obtained within the power industry of which 3 + years must have been at a management level pertaining to customer service",
AssignedToName: "Yvette Rautenbach",
EmailJobApplicationTo: "jobs@adogreen.com",
Salary: "Negotiable Depending on Experience + Benefits ",
Status: "open",
StatusDescription: "",
Published: "yes",
Keywords: "Manager, Service Delivery",
ContractType: "Permanent",
Sector: "Power Generation",
PnetSector: "Automotive Sector",
Area: "Central Africa",
Country: "South Africa",
Province: "",
Location: "Gaborone",
Description: "Eskom and other countries – Structured. P60K – P65K TCTC Open Negotiable and flexible. – tax bracket differences 25% tax – R75K – R90K CTC per month plus housing and bonus (Dep on exp) Housing given MA, pension/gratuity - of basic plus car allowance 25 of that collectively Cell phone included in there 13th Cheque no – only performance bonus – 30% of one annual basic salary",
ShortDescription: "In reporting to the General Manager, the successful incumbent will be managing and improving all the relevant processes and activities with regards to the delivery of customer services, distribution systems and maintenance. They will be required to build connections and ensure product development within the product area of operation. He/she will need to maintain the customer database.",
OpenDate: "01/05/2018",
StartDate: "",
ImageUploadedId: "",
CloseDate: "",
Abstract: "Customer Services & Supply Manager - open",
TimeStamp: "2018-05-02 13:38:40+02"
}
],
""
]

1 个答案:

答案 0 :(得分:0)

我不知道问题是否真的在您的排序中...我尝试了与您完全相同的代码,并且我自己编辑了数组中的json。

我试过这段代码:

$array = [
    "Job",
    "",
    array (
        '0' => array(
            'AppDataId' => 144150,
            'JobTitle' => "Network Maintenance Manager",
            'Location' => "Gaborone",
            'OpenDate' => "04/05/2018",
            'Abstract' => "Network Maintenance Manager - open",
            'TimeStamp' => "2018-05-02 16 =>17 =>31+02"
        ),
        '1' => array(
            'AppDataId' => 144129,
            'JobTitle' => "Network Development Manager",
            'Location' => "Gaborone",
            'OpenDate' => "10/05/2018",
            'Abstract' => "Network Development Manager - open",
            'TimeStamp' => "2018-05-02 15 =>58 =>56+02"
        ),
        array(
            'AppDataId' => 144087,
            'JobTitle' => "Customer Services & Supply Manager",
            'Location' => "Gaborone",
            'OpenDate' => "01/05/2018",
            'Abstract' => "Customer Services & Supply Manager - open",
            'TimeStamp' => "2018-05-02 13 =>38 =>40+02"
        )
    ),
    ""
];


uSort($array[2], function(array $a, array $b){
    return strtotime($a['OpenDate']) - strtotime($b['OpenDate']);
});
var_dump($array);

得到了这个结果:

array(4) {
    [0]=>
    string(3) "Job"
    [1]=>
    string(0) ""
    [2]=>
    array(3) {
        [0]=>
        array(6) {
            ["AppDataId"]=>
            int(144087)
            ["JobTitle"]=>
            string(34) "Customer Services & Supply Manager"
            ["Location"]=>
            string(8) "Gaborone"
            ["OpenDate"]=>
            string(10) "01/05/2018"
            ["Abstract"]=>
            string(41) "Customer Services & Supply Manager - open"
            ["TimeStamp"]=>
            string(26) "2018-05-02 13 =>38 =>40+02"
        }
        [1]=>
        array(6) {
            ["AppDataId"]=>
            int(144150)
            ["JobTitle"]=>
            string(27) "Network Maintenance Manager"
            ["Location"]=>
            string(8) "Gaborone"
            ["OpenDate"]=>
            string(10) "04/05/2018"
            ["Abstract"]=>
            string(34) "Network Maintenance Manager - open"
            ["TimeStamp"]=>
            string(26) "2018-05-02 16 =>17 =>31+02"
        }
        [2]=>
        array(6) {
            ["AppDataId"]=>
            int(144129)
            ["JobTitle"]=>
            string(27) "Network Development Manager"
            ["Location"]=>
            string(8) "Gaborone"
            ["OpenDate"]=>
            string(10) "10/05/2018"
            ["Abstract"]=>
            string(34) "Network Development Manager - open"
            ["TimeStamp"]=>
            string(26) "2018-05-02 15 =>58 =>56+02"
        }
    }
    [3]=>
    string(0) ""
}

正确排序