如何在php

时间:2019-06-19 17:23:03

标签: php arrays sorting object

我无法对该对象数组进行排序。请帮助。 我正在使用usort对数组进行排序。

我也收到此错误:

  

PHP解析错误:语法错误,意外的'=>'(T_DOUBLE_ARROW),期望在第7行的/home/cg/root/128245/main.php中出现')'

我需要按照班级的 job_name 属性的顺序对该数组进行排序。

<?php 
$gfg_array =Array
(
    [0] => Array
        (
            [status] => SUCCESS
            [duration] => 04:32:22
            [date] => 13-Jun-2019
            [start_stamp] => 02:29:32
            [type] => implicit
            [job_name] => build-bbjsd
        )

    [1] => Array
        (
            [status] => SUCCESS
            [change_sets] => Array
                (
                )
                [job_id] =>  # 4156
            [url] => http://www.html.com
            [duration] => 0:39:25
            [test_script] => {"tc_results": [{"status": "pass", "testcase_time": "0:01:06"}]} 
            [date] => 2019-06-13
            [start_stamp] => 9:30:34
            [type] => explicit
            [job_name] => json-dhj
        )

    [2] => Array
        (
            [status] => SUCCESS
            [duration] => 
            [date] => 
            [start_stamp] => 
            [type] => implicit
            [job_name] => ci-ulp

    [3] => Array
        (
            [status] => DISABLED
            [duration] => 
            [date] => 
            [start_stamp] => 
            [type] => implicit
            [job_name] => build-c201-qemux86-64
        )

    [4] => Array
        (
            [status] => SUCCESS
            [duration] => 04:30:30
            [date] => 13-Jun-2019
            [start_stamp] => 02:29:15
            [type] => implicit
            [job_name] => build-fss-image-full-l200-qemux86-64
        )

    [5] => Array
        (
            [status] => SUCCESS
            [change_sets] => Array
                (
                )

            [job_id] =>  # 4156
            [url] => http://www.html.com
            [duration] => 0:39:25
            [test_script] => {"tc_results": [{"status": "pass", 
            "testcase_time": "0:01:06"}]} 
            [date] => 2019-06-13
            [start_stamp] => 9:30:34
            [type] => explicit
            [job_name] => json-ci-ulp-fss-image-full-l200-qemux86-64
        )
)

function querySort ($x, $y) {
   return strcasecmp($x['job_name'], $y['job_name']);
}

usort($gfg_array, 'querySort');


print_r($gfg_array)
?>

2 个答案:

答案 0 :(得分:1)

使用usort功能。 您需要创建一个规则,使您的对象可以相互比较。 它是如何使用的:

function compareObjects( $a , $b )
{
    return strcmp( $a->job_name , $b->job_name );
}

usort( $gfg_array , "compareObjects" );

此外,您的代码中存在语法错误,您需要将数组值设置为字符串,如下所示:

<?php 
$gfg_array =Array
(
    [0] => Array
        (
            [status] => "SUCCESS"
            [duration] => "04:32:22"
            [date] => "13-Jun-2019"
            [start_stamp] => "02:29:32"
            [type] => "implicit"
            [job_name] => "build-bbjsd"
        )

    [1] => Array
        (
            [status] => "SUCCESS"
            [change_sets] => Array
                (
                )
                [job_id] =>  "# 4156"
            [url] => "http://www.html.com"
            [duration] => "0:39:25"
            [test_script] => {"tc_results": [{"status": "pass", "testcase_time": "0:01:06"}]} 
            [date] => "2019-06-13"
            [start_stamp] => "9:30:34"
            [type] => "explicit"
            [job_name] => "json-dhj"
        )

    [2] => Array
        (
            [status] => "SUCCESS"
            [duration] => ""
            [date] => ""
            [start_stamp] => ""
            [type] => "implicit"
            [job_name] => "ci-ulp"

    [3] => Array
        (
            [status] => "DISABLED"
            [duration] => ""
            [date] => ""
            [start_stamp] => ""
            [type] => "implicit"
            [job_name] => "build-c201-qemux86-64"
        )

    [4] => Array
        (
            [status] => "SUCCESS"
            [duration] => "04:30:30"
            [date] => "13-Jun-2019"
            [start_stamp] => "02:29:15"
            [type] => "implicit"
            [job_name] => "build-fss-image-full-l200-qemux86-64"
        )

    [5] => Array
        (
            [status] => "SUCCESS"
            [change_sets] => Array
                (
                )

            [job_id] =>  "# 4156"
            [url] => "http://www.html.com"
            [duration] => "0:39:25"
            [test_script] => {"tc_results": [{"status": "pass", 
            "testcase_time": "0:01:06"}]} 
            [date] => "2019-06-13"
            [start_stamp] => "9:30:34"
            [type] => "explicit"
            [job_name] => "json-ci-ulp-fss-image-full-l200-qemux86-64"
        )
)

function querySort ($x, $y) {
   return strcasecmp($x['job_name'], $y['job_name']);
}

usort($gfg_array, 'querySort');


print_r($gfg_array)
?>

答案 1 :(得分:0)

您可以尝试使用此功能。

    function multisortByKeyValue( $k, $arr ) {
        $ids   = array();
        $index = 1;

        foreach ( $arr as $key => $row ) {
            $ids[ $key ] = intval( $row[ $k ] ) . '-' . $index . '-' . $key;
            $index ++;
        }

        natsort( $ids );

        $arr = array_merge( $ids, $arr );

        return $arr;
    }
```