dongxing2302 2015-12-04 17:09
浏览 45
已采纳

PHP JSON从关联数组STUCK中排序

Good day to those smarter than me. I've been all over google and here to no avail. Here's my situation... I have a json file with this:

    {
"firstset": {
    "xgroup": [
    {
        "order": 3,
        "title": "third in xgroup"
    }, {
        "order": 5,
        "title": "fifth in xgroup"
    }, {
        "order": 4,
        "title": "fourth in xgroup"
    }, {
        "order": 1,
        "title": "first in xgroup"
    }, {
        "order": 2,
        "title": "second in xgroup"
    }
    ]
},
"secondset": {
    "ygroup": [
    {
        "order": 7,
        "title": "seventh in ygroup"
    }, {
        "order": 4,
        "title": "fourth in ygroup"
    }, {
        "order": 6,
        "title": "sixth in ygroup"
    }, {
        "order": 1,
        "title": "first in ygroup"
    }, {
        "order": 3,
        "title": "third in ygroup"
    }, {
        "order": 2,
        "title": "second in ygroup"
    }, {
        "order": 8,
        "title": "eighth in ygroup"
    }, {
        "order": 5,
        "title": "fifth in ygroup"  
    }
    ]
}

}

and this is the PHP:

    $json_url = "js/testlist.json";
    $json = file_get_contents($json_url);
    $data = json_decode($json, TRUE);

    echo '<ul>';
    foreach ($data['firstset']['xgroup'] as $val) {
        echo '<li>' . $val['order'] . '.) ' . $val['title'] . '</li>';
    }
    echo '</ul>';

I've tried everything from the old fashioned 'my_sort' functions found on here to just usorting the entire array with no avail. Is there a way to sort by the 'order' field on the 'firstset' array and display the data?

Thank you in advance...

  • 写回答

1条回答 默认 最新

  • dongsi073898 2015-12-04 17:23
    关注

    To expand on my comment, here's what I'd do:

    $data = json_decode($json, true);
    //array_column: use values of order key as keys, title key as value
    $data['firstset']['xgroup'] = array_column(
        $data['firstset']['xgroup'],
        'title',//pass null here to assign the entire array to the order key (ie [order => x, title => some title])
        'order'
    );
    //sort the result by key
    ksort($data['firstset']['xgroup']);
    foreach ($data['firstset']['xgroup'] as $order => $title) {
        //use vars here
    }
    

    Demo here

    This requires PHP 5.5 or higher, so if you're running PHP 5.4, just write a simple loop:

    $ordered = array();
    foreach ($data['firstset']['xgroup'] as $arr) {
        $ordered[$arr['order']] = $arr['title'];
    }
    ksort($ordered);
    foreach ($ordered as $order => $title) {
        //same as before
    }
    

    There is a PHP implementation that is (AFAIK) fully compatible with the native array_column function on github

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘