关于一些 json 的问题

2017-02-12 10:24:50 +08:00
 byxiaozhe
[{"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"}
]
},

的这个形式 前面相同然后合并在一起
2278 次点击
所在节点    PHP
14 条回复
onlyhot
2017-02-12 10:31:21 +08:00
转换成数组处理
imlonghao673
2017-02-12 10:36:14 +08:00
最后面的
},
前面有?
byxiaozhe
2017-02-12 10:41:42 +08:00
@onlyhot 源代码是数组 但是不知道数组怎么搞
byxiaozhe
2017-02-12 10:42:24 +08:00
@imlonghao673 fupdz 前面那个{就是 只是后面忘记加了个]
mhycy
2017-02-12 10:43:10 +08:00
唯一方式:遍历重构
qiayue
2017-02-12 10:44:39 +08:00
循环,然后判断 fupdz 一致则放一起
byxiaozhe
2017-02-12 10:45:43 +08:00
@qiayue 循环采用的是数据库的 while
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
2017-02-12 10:53:48 +08:00
@mhycy 我试试
byxiaozhe
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
2017-02-12 11:40:22 +08:00
@byxiaozhe #10 我只能说你这个代码写的真是 666 啊
mhycy
2017-02-12 12:16:06 +08:00
@byxiaozhe

。。。。你这直接改数据库查询不就完了
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
2017-02-12 14:04:25 +08:00
v2 的回复功能太坑,不支持 markdown 还把代码缩进给吃了....

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/339860

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX