V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
byxiaozhe
V2EX  ›  PHP

关于一些 json 的问题

  •  
  •   byxiaozhe · 2017-02-12 10:24:50 +08:00 · 2257 次点击
    这是一个创建于 2833 天前的主题,其中的信息可能已经有所发展或是发生改变。
    [{"fupdz":"36","data":[{"id":"37","fup":"36","name":"\u5546\u5bb6\u70b9\u8bc4","px":"4","icon":"a5\/common_37_icon.png"}]},{"fupdz":"36","data":[{"id":"38","fup":"36","name":"\u623f\u5c4b\u79df\u552e","px":"5","icon":"a5\/common_38_icon.png"}]},


    如何改成
    [
    {"fupdz":"36","data":[
    {"id":"37","fup":"36","name":"\u5546\u5bb6\u70b9\u8bc4","px":"4","icon":"a5\/common_37_icon.png"},
    {"id":"38","fup":"36","name":"\u623f\u5c4b\u79df\u552e","px":"5","icon":"a5\/common_38_icon.png"}
    ]
    },

    的这个形式 前面相同然后合并在一起
    14 条回复    2017-02-12 14:04:25 +08:00
    onlyhot
        1
    onlyhot  
       2017-02-12 10:31:21 +08:00 via iPhone
    转换成数组处理
    imlonghao673
        2
    imlonghao673  
       2017-02-12 10:36:14 +08:00 via Android
    最后面的
    },
    前面有?
    byxiaozhe
        3
    byxiaozhe  
    OP
       2017-02-12 10:41:42 +08:00
    @onlyhot 源代码是数组 但是不知道数组怎么搞
    byxiaozhe
        4
    byxiaozhe  
    OP
       2017-02-12 10:42:24 +08:00
    @imlonghao673 fupdz 前面那个{就是 只是后面忘记加了个]
    mhycy
        5
    mhycy  
       2017-02-12 10:43:10 +08:00
    唯一方式:遍历重构
    qiayue
        6
    qiayue  
       2017-02-12 10:44:39 +08:00
    循环,然后判断 fupdz 一致则放一起
    byxiaozhe
        7
    byxiaozhe  
    OP
       2017-02-12 10:45:43 +08:00
    @qiayue 循环采用的是数据库的 while
    mhycy
        8
    mhycy  
       2017-02-12 10:52:06 +08:00
    function convertFupdzData($source) {
    $fupdz_arr = [];

    foreach($source as $item) {
    if(array_key_exists($item['fupdz'], $fupdz_arr)) {
    $fupdz_arr[$item['fupdz']][] = $item;
    } else {
    $fupdz_arr[$item['fupdz']] = [$item];
    }
    }

    $result = [];
    foreach($fupdz_arr as $fupdz => $data) {
    $result[] = [
    "fupdz" => strval($fupdz),
    "data" => $data
    ];
    }

    return $result;
    }
    byxiaozhe
        9
    byxiaozhe  
    OP
       2017-02-12 10:53:48 +08:00
    @mhycy 我试试
    byxiaozhe
        10
    byxiaozhe  
    OP
       2017-02-12 10:59:25 +08:00
    @mhycy

    $query = DB::query("SELECT * FROM " . DB::table('forum_forum') . " WHERE type='group' and status=1 ORDER BY fid ASC");
    $ar=array();
    while ($soft = DB::fetch($query)) {
    $querys = DB::query("SELECT * FROM " . DB::table('forum_forum') . " WHERE fup=".$soft['fid']." and status=1 ORDER BY fid ASC");
    while ($softs = DB::fetch($querys)) {
    $queryss = DB::query("SELECT * FROM " . DB::table('forum_forumfield') . " WHERE fid=".$softs['fid']." ORDER BY fid ASC");
    while ($softss = DB::fetch($queryss)) {
    $ar[] = array("fupdz"=>$soft['fid'],"data"=>array(array("id"=>$softs['fid'],"fup"=>$softs['fup'],"name"=>iconv('gb2312','utf-8',$softs['name']),"px"=>$softs['displayorder'],"icon"=>$softss['icon'])));
    }
    }
    }


    如何添加你刚发的那个 这是源代码
    m939594960
        11
    m939594960  
       2017-02-12 11:40:22 +08:00
    @byxiaozhe #10 我只能说你这个代码写的真是 666 啊
    mhycy
        12
    mhycy  
       2017-02-12 12:16:06 +08:00
    @byxiaozhe

    。。。。你这直接改数据库查询不就完了
    fytriht
        13
    fytriht  
       2017-02-12 14:02:50 +08:00
    ```javascript
    const sort = (arr) => {
    let hash = {}
    let newArr = []
    let i = 0
    for (let item of foo) {
    if (hash[item.fupdz] == undefined) {
    hash[item.fupdz] = i
    newArr.push(item)
    i++
    } else {
    const idx = hash[item.fupdz]
    const data = item.data
    data.forEach(item => newArr[idx].data.push(item))
    }
    }
    return newArr
    }
    ```
    这样写应该没问题,楼主试试
    fytriht
        14
    fytriht  
       2017-02-12 14:04:25 +08:00
    v2 的回复功能太坑,不支持 markdown 还把代码缩进给吃了....
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5508 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 06:55 · PVG 14:55 · LAX 22:55 · JFK 01:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.