0

I am not able to sort this array of object. please kindly help. I am using usort for sorting the array.

Also I am getting this error:

PHP Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')' in /home/cg/root/128245/main.php on line 7

I need to sort this array by the order of job_name attribute of my class.

<?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)
?>
Serg
  • 2,346
  • 3
  • 29
  • 38
Jeevanjyothi
  • 9
  • 1
  • 3
  • 8

2 Answers2

0

Use usort function. You need to create a rule that your objects can be compared with each other. It is how it's used:

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

usort( $gfg_array , "compareObjects" );

Also you have a syntax error in your code and you need to set array values as strings as below:

<?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)
?>
Alireza A2F
  • 519
  • 4
  • 26
  • is that array is correct.when am trying to run its showing :PHP Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')' in /home/cg/root/128245/main.php on line 7 – Jeevanjyothi Jun 19 '19 at 17:39
  • 1
    I guess you're using `=>` instead of `->` – Alireza A2F Jun 19 '19 at 17:41
  • am not using from mongo its coming – Jeevanjyothi Jun 19 '19 at 17:42
  • 1
    I don't get what your problem is. – Alireza A2F Jun 19 '19 at 17:53
  • when i am using $gfg_array1 =Array ( Array ( 'score' => '100', 'name' => 'b', 'subject' => 'Data Structures' ), Array ( 'score' => '100', 'name' => 'a', 'subject' => 'Data Structures' ) ); sorting is working fine – Jeevanjyothi Jun 19 '19 at 18:21
  • but for $gfg_array =Array ( [0] => Array ( [score] => 100 [name] => b [subject] => Data Structures ) [1] => Array ( [score] => 100 [name] => a [subject] => Data Structures ) ); sorting is not working – Jeevanjyothi Jun 19 '19 at 18:21
  • 1
    Your Array structure is wrong in the second case. Why don't you use it like the first case that works? – Alireza A2F Jun 19 '19 at 18:44
  • 1
    You need to add double quotes before ` SUCCESS` and time and other values you're adding to the array. – Alireza A2F Jun 19 '19 at 20:44
0

You can try this function..

    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;
    }
```
Anshu
  • 1,277
  • 2
  • 13
  • 28