T
T
trall2014-06-12 10:21:11
PHP
trall, 2014-06-12 10:21:11

How to alphabetically sort json in php?

There is this json

Press me, press me!
{
    "pagination": {},
    "meta": {
        "code": 200
    },
    "data": [{
        "username": "google",
        "bio": "Google unfiltered\u2014sometimes with filters.",
        "website": "http:\/\/www.google.com",
        "profile_picture": "http:\/\/photos-h.ak.instagram.com\/hphotos-ak-xaf1\/1040179806947_a.jpg",
        "full_name": "Google",
        "id": "1067259270"
    }, {
        "username": "natfsefa_zofelotarfesfesf",
        "bio": "",
        "website": "",
        "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/profile_13171604287.jpg",
        "full_name": "",
        "id": "1317160474"
    }, {
        "username": "wifesgt_joy",
        "bio": "Dance, dance, dance, dance, dance to the radio...",
        "website": "",
        "profile_picture": "http:\/\/photos-g.ak.instagram.com\/hphotos-ak-xpa1\/10454101_a.jpg",
        "full_name": "dima",
        "id": "496709776"
    }, {
        "username": "kotifjyfj",
        "bio": "new york, moscow, baby jennifer, monya the cat and all the little things that make life great, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0432\u0435\u0441\u0442\u044c \u0411\u0435\u043b\u043a\u0438\u043d\u0430",
        "website": "",
        "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/prof3992.jpg",
        "full_name": "",
        "id": "10615275"
    }, {
        "username": "pepperfyjfyj",
        "bio": "",
        "website": "",
        "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/profile_22968593180.jpg",
        "full_name": "\u0410\u043b\u0435\u043a\u0441\u0435\u0439 \u041f\u0435\u0440\u0446\u043e\u0432\u0441\u043a\u0438\u0439",
        "id": "229620782"
    }, {
        "username": "orbitalnaya_bgyjgyj",
        "bio": "",
        "website": "",
        "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/pr80532.jpg",
        "full_name": "\u0411\u0443\u0433\u0443\u0440\u0442",
        "id": "1209809871"
    }, {
        "username": "whereisihj",
        "bio": "",
        "website": "http:\/\/www.adtr.com",
        "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/pr80802.jpg",
        "full_name": "A Day to Remember",
        "id": "210808091"
    }, {
        "username": "ktbasfsrg",
        "bio": "",
        "website": "",
        "profile_picture": "http:\/\/photos-e.ak.instagram.com\/hphotos-ak-xaf1\/10358184_509696004_a.jpg",
        "full_name": "Katya",
        "id": "1080491543"
    }, {
        "username": "redheadandmad",
        "bio": "",
        "website": "",
        "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/prof595.jpg",
        "full_name": "",
        "id": "39519313"
    }, {
        "username": "alinasegzsfef",
        "bio": "Hello!\ud83d\udc4b \nMy n\ud83d\udc96 20.08.  My Hob... Follow me.\ud83d\ude0a",
        "website": "",
        "profile_picture": "http:\/\/photos-d.ak.instagram.com\/hphotos-ak-xpa1\/104016685986445_a.jpg",
        "full_name": "\ud83c\udf1f\u0410\u043b\u0438\u043d\u0430\ud83c\udf1f",
        "id": "508962984"
    }, {
        "username": "useregsg",
        "bio": "#dropsydies",
        "website": "http:\/\/vk.com\/dropsydies",
        "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/prof792477.jpg",
        "full_name": "Igor Belkin",
        "id": "10721268"
    }, {
        "username": "tanya_fefz",
        "bio": "",
        "website": "",
        "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/profile_95.jpg",
        "full_name": "\u0422\u0430\u043d\u044f",
        "id": "470172978"
    }, {
        "username": "valeria_behrdh",
        "bio": "",
        "website": "",
        "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/anonymousUser.jpg",
        "full_name": "\u0412\u0430\u043b\u0435\u0440\u0438\u044f",
        "id": "366715377"
    }, {
        "username": "lukinzsdsz",
        "bio": "Sd83d\ude09",
        "website": "",
        "profile_picture": "http:\/\/photos-c.ak.instagram.com\/hpho82_a.jpg",
        "full_name": "",
        "id": "256043671"
    }, {
        "username": "recordsahftjh",
        "bio": "DANCE WE LOVE SAKHALIN",
        "website": "http:\/\/record.sakh.fm",
        "profile_picture": "http:\/\/images.ak.instagram.com\/profiles4073927850.jpg",
        "full_name": "\u0420\u0435\u043a\u043e\u0440\u0434 \u0420\u0435\u043a\u043e\u0440\u0434\u044b\u0447 88.9 fm",
        "id": "400529998"
    }, {
        "username": "arinfesfes",
        "bio": "",
        "website": "",
        "profile_picture": "http:\/\/photos-a.ak.instagram.com\/hphotos-ak0442_a.jpg",
        "full_name": "",
        "id": "184345217"
    }, {
        "username": "_loofesg",
        "bio": "",
        "website": "",
        "profile_picture": "http:\/\/images.ak.instagram.com\/profilee_2790979.jpg",
        "full_name": "no.",
        "id": "279036847"
    }, {
        "username": "vntsesfesf",
        "bio": "",
        "website": "",
        "profile_picture": "http:\/\/photos-c.ak.instagram.com\/hphotos-ak-xa4120_a.jpg",
        "full_name": "",
        "id": "278813911"
    }, {
        "username": "stasyfesges",
        "bio": "",
        "website": "",
        "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/profile888131.jpg",
        "full_name": "Stesfesfova",
        "id": "247108881"
    }, {
        "username": "enesefsf",
        "bio": "\u041d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043a\u0440\u0430\u0441\u0438\u0442\u0435\u043b\u0435\u0439 \u0438 \u043a\u043e\u043d\u0441\u0435\u0440\u0432\u0430\u043d\u0442\u043e\u0432",
        "website": "",
        "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/anonymousUser.jpg",
        "full_name": "Cj",
        "id": "36061426"
    }, {
        "username": "tsyplfesfin",
        "bio": "\u041f\u0440\u0435\u0441\u0441-\u0441\u0435\u043a\u0440\u0435\u0442\u0430\u0440\u044c N1",
        "website": "",
        "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/profil85270.jpg",
        "full_name": "Vladsefeslukhin",
        "id": "2607286"
    }, {
        "username": "afeskov",
        "bio": "\u041a\u0440\u0435\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u0432\u0435\u0434\u0443\u0449\u0438\u0439, \u041c\u0421, \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u0438 \u043f\u0440\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043c\u0435\u0440\u043e\u043f\u0440\u0438\u044f\u0442\u0438\u0439 89241910010",
        "website": "",
        "profile_picture": "http:\/\/images.ak.instagram.com\/profiles\/profile_61.jpg",
        "full_name": "Vikаbkov",
        "id": "29295817"
    }]
}


But with this code I bring it to a readable form
<?php
$json = json_decode(file_get_contents('json.txt'));
foreach ($json->data as $item) {
    echo $item->username;
    echo '<br>';
}
?>

How to sort alphabetically (az) by username in PHP?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vit, 2014-06-12
@sashablashenkov

Use usort ( www.php.net/manual/ru/function.usort.php) You
can pass your comparator function to it, in which you compare the username fields.
Here is an example of code that solves your problem:

<?php

$json = file_get_contents('test.json');
$data = json_decode($json, true);

usort($data['data'], function($a, $b){
  if ($a['username'] < $b['username']) {
    return -1;
  }

  if ($a['username'] > $b['username']) {
    return 1;
  }

  return 0;
});

print_r($data);

L
lnked, 2014-06-12
@lnked

Try like this

$json = json_decode( file_get_contents('json.txt'), true );
$json = sortmulti( $json, 'username',  'asc',  );

foreach ($json->data as $item) {
    echo $item->username;
    echo '<br>';
}
?>

the function itself
function sortmulti ($array, $index, $order, $natsort=FALSE, $case_sensitive=FALSE) 
{
    if(is_array($array) && count($array)>0)
    {
        foreach(array_keys($array) as $key)
        $temp[$key]=$array[$key][$index];
        if(!$natsort)
        {
            if ($order=='asc')
                asort($temp);
            else   
                arsort($temp);
        }
        else
        {
            if ($case_sensitive===true)
                natsort($temp);
            else
                natcasesort($temp);
                
            if($order!='asc')
                $temp=array_reverse($temp,TRUE);
        }
        foreach(array_keys($temp) as $key)
            if (is_numeric($key))
                $sorted[]=$array[$key];
            else   
                $sorted[$key]=$array[$key];
        return $sorted;
    }
    
    return $sorted;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question