-1

Here is a simplified version of the array. It is easy to sort this by 'id', or 'created_by', but I'm having a hard time figuring out how to sort it by the value of "e5e53240-1d5a-4b50-ad7d-cfa00f33badd" inside the 'elements' object. I've spent hours searching through other questions trying to figure this out, but with no luck.

So here's the simplified version of the array via print_r:

[0] => stdClass Object
    (
        [id] => 12
        [created_by] => 776
        [searchable] => 1
        [elements] =>  {
"b2c4ecaa-f68f-4a5c-a551-339aa8f01421":  {

},


"e5e53240-1d5a-4b50-ad7d-cfa00f33badd":  {
    "0":  {
        "value": "Aeronca"
    }
},

"d7c903a8-fa15-4620-9d9b-2238cb48fd5c":  {
    "0":  {
        "value": "L-3B"
    }
}

    )

[1] => stdClass Object
    (
        [id] => 21
        [created_by] => 776
        [searchable] => 1
        [elements] =>  {
"b2c4ecaa-f68f-4a5c-a551-339aa8f01421":  {

},


"e5e53240-1d5a-4b50-ad7d-cfa00f33badd":  {
    "0":  {
        "value": "BEECHCRAFT"
    }
},

"d7c903a8-fa15-4620-9d9b-2238cb48fd5c":  {
    "0":  {
        "value": "N-35"
    }
}

    )

[2] => stdClass Object
    (
        [id] => 13
        [created_by] => 776
        [searchable] => 1
        [elements] =>  {
"b2c4ecaa-f68f-4a5c-a551-339aa8f01421":  {

},

"e5e53240-1d5a-4b50-ad7d-cfa00f33badd":  {
    "0":  {
        "value": "AEROSPORT"
    }
},

"d7c903a8-fa15-4620-9d9b-2238cb48fd5c":  {
    "0":  {
        "value": ""
    }
}

    )

So basically the array, when sorted, should have the first element be where it is now, followed by the third element, followed by the second, so it would look like this:

[0] => stdClass Object
    (
        [id] => 12
        [created_by] => 776
        [searchable] => 1
        [elements] =>  {
"b2c4ecaa-f68f-4a5c-a551-339aa8f01421":  {

},


"e5e53240-1d5a-4b50-ad7d-cfa00f33badd":  {
    "0":  {
        "value": "Aeronca"
    }
},

"d7c903a8-fa15-4620-9d9b-2238cb48fd5c":  {
    "0":  {
        "value": "L-3B"
    }
}

    )

[1] => stdClass Object
    (
        [id] => 13
        [created_by] => 776
        [searchable] => 1
        [elements] =>  {
"b2c4ecaa-f68f-4a5c-a551-339aa8f01421":  {

},

"e5e53240-1d5a-4b50-ad7d-cfa00f33badd":  {
    "0":  {
        "value": "AEROSPORT"
    }
},

"d7c903a8-fa15-4620-9d9b-2238cb48fd5c":  {
    "0":  {
        "value": ""
    }
}

    )

[2] => stdClass Object
    (
        [id] => 21
        [created_by] => 776
        [searchable] => 1
        [elements] =>  {
"b2c4ecaa-f68f-4a5c-a551-339aa8f01421":  {

},


"e5e53240-1d5a-4b50-ad7d-cfa00f33badd":  {
    "0":  {
        "value": "BEECHCRAFT"
    }
},

"d7c903a8-fa15-4620-9d9b-2238cb48fd5c":  {
    "0":  {
        "value": "N-35"
    }
}

    )

Keep in mind that the actual array is far more complex, and contains close to 1,000 elements in the array, so hopefully whatever method I use would minimize load on the server.

2 Answers 2

1

Something like this to decode the json and create a key array. Then use the key array and array_multisort() to sort by whatever order you want:

foreach($object as $k => $o) {
    //PHP 5.4.0
    //$key[$k] = json_decode($o->elements, true)['e5e53240-1d5a-4b50-ad7d-cfa00f33badd'][0]['value'];
    $elements = json_decode($o->elements, true);
    $key[$k]  = $elements['e5e53240-1d5a-4b50-ad7d-cfa00f33badd'][0]['value'];
}
Sign up to request clarification or add additional context in comments.

Comments

0

You want to create your own comparator function and use usort for sorting.

For example:

function myComparator($a, $b) {
    return  $a->someValue - $b->someValue;
}

The return value explained in php.net:

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

And somewhere in your code where you want to sort the array:

usort($arrayOfObjects, 'myComparator');

1 Comment

I did the following based on your answer, and it works like a charm: function myComparator($a, $b) { $json_var = json_decode($a->elements, true); $first = $json_var['e5e53240-1d5a-4b50-ad7d-cfa00f33badd'][0]['value']; $json_var = json_decode($b->elements, true); $second = $json_var['e5e53240-1d5a-4b50-ad7d-cfa00f33badd'][0]['value']; return strcmp ( $first , $second ); } usort($arrayOfObjects, 'myComparator');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.