0

I've looked a little and found an answer that partially does what I am interested in doing, see: Sorting a JSON array in PHP

I have some decoded JSON that looks like this, just a sample.

{
    "status": "OK",
    "page": {
        "rows": 5000,
        "more": 0,
        "number": 1
    },
    "accounts": [
        {
            "connected": 0,
            "settings": {
                "link_first_study_only": "0",
                "update_study_source_on_notify": "1",
                "link_external_whitelist": "",
                "other_ingress_tags": ""
            },
            "must_approve_upload": 0,
            "css": null,
            "share_via_gateway": 0,
            "password_expire": 90,
            "vanity": "medpics"

        }
    ]
}

What I would like to do is sort everything alphabetically so that it is easier to read and uniform. So that what I would see is:

{
    "accounts": [
        {
            "css": null,
            "connected": 0,
            "must_approve_upload": 0,
            "password_expire": 90,
            "settings": {

                "link_external_whitelist": "",
                "link_first_study_only": "0",
                "other_ingress_tags": "",
                "update_study_source_on_notify": "1"
            },

            "share_via_gateway": 0,
            "vanity": "medpics"
        }
    ],
    "page": {
        "more": 0,
        "number": 1,
        "rows": 5000,
    }
    "status": "OK"
}

Every element is sorted alphabetically. Is that possible ?

Community
  • 1
  • 1
SScotti
  • 2,158
  • 4
  • 23
  • 41
  • there is no array, only objects – Honk der Hase Jul 20 '16 at 19:12
  • @LarsStegelitz In PHP they would be associative arrays. – Barmar Jul 20 '16 at 19:15
  • Write a recursive function that uses `ksort()`. It will need to take the array argument as a reference parameter. – Barmar Jul 20 '16 at 19:16
  • Why isn't the `settings` array sorted alphabetically in your desired result? – Barmar Jul 20 '16 at 19:17
  • JSON is a data exchange format, not for people to casually read. What purpose would the complex recursive algorithm to do this solve other than vanity? – Jeremy Harris Jul 20 '16 at 19:22
  • Sorry, I edited the desired result. Also, it really is for vanity and development. I want to easily look at the responses that I get and have the elements always in the same order. It isn't that way by default. – SScotti Jul 20 '16 at 19:32

1 Answers1

0

Pretty straightforward

$json = <<<JSON
{
    "status": "OK",
    "page": {
        "rows": 5000,
        "more": 0,
        "number": 1
    },
    "accounts": [
        {
            "connected": 0,
            "settings": {
                "link_first_study_only": "0",
                "update_study_source_on_notify": "1",
                "link_external_whitelist": "",
                "other_ingress_tags": ""
            },
            "must_approve_upload": 0,
            "css": null,
            "share_via_gateway": 0,
            "password_expire": 90,
            "vanity": "medpics"

        }
    ]
}
JSON;

$json = json_decode($json, true);

function ksort_recursive(&$array) {
    ksort($array);
    foreach ($array as &$value) {
        if (is_array($value)) {
            ksort_recursive($value);
        }
    }
}

ksort_recursive($json);

print_r($json);

Proof of solution here

https://3v4l.org/qUAA0

Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
  • That will work. It is a kind of vanity and development thing, but it makes it really easy to visually compare records for responses that have the same structure but different data. Thanks. – SScotti Jul 20 '16 at 20:02