Skip to content

Instantly share code, notes, and snippets.

@manchan
Created November 20, 2015 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manchan/50328fd3443abdbb5ae4 to your computer and use it in GitHub Desktop.
Save manchan/50328fd3443abdbb5ae4 to your computer and use it in GitHub Desktop.
Chromeでjsonのkeyを数字にしている場合、勝手に昇順にされる件。 ref: http://qiita.com/you_matz/items/c046e9070f211a8bb293
{
1444275582: {
url: "https://instagram.com/p/8j9S8_r_BE/",
img: "https://scontent.cdninstagram.com/hphotos-xap1/t51.2885-15/s640x640/sh0.08/e35/12120292_917670381655742_1569021259_n.jpg"
},
1444545530: {
url: "https://instagram.com/p/8sALlWAfUJ/",
img: "https://scontent.cdninstagram.com/hphotos-xtp1/t51.2885-15/e15/12106290_1294775143882158_1454631624_n.jpg"
}
}
{
{
created_at: "1447984282",
url: "https://instagram.com/p/-SfFKzy73D/",
img: "https://scontent.cdninstagram.com/hphotos-xfp1/t51.2885-15/ s640x640/sh0.08/e35/12256594_516826635166064_1483786264_n.jpg"
},
{
created_at: "1447984163",
url: "https://instagram.com/p/-Se2neyurm/",
img: "https://scontent.cdninstagram.com/hphotos-xfp1/t51.2885-15/e15/12230899_1538400876451449_364138165_n.jpg"
}
}
<?php
// APIから取得したデータを成形
foreach($result["data"] as $k => $v){
$insta_arr[$k]["created_at"] = $v["created_time"];
$insta_arr[$k]["url"] = $v["link"];
$insta_arr[$k]["img"] = $v["images"]["standard_resolution"]["url"];
//いったんソートするキーを配列で取り出して、
$created_at_arr[$k] = $v["created_time"];
}
// keyで降順
array_multisort($created_at_arr ,SORT_DESC,$insta_arr);
echo json_encode($insta_arr);
exit;
_.map(_.sortByOrder(insta_arr, ['created_at'], ['desc']), _.values);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment