質問編集履歴
1
H27\.10\.18 15:30 追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -168,21 +168,43 @@
|
|
168
168
|
|
169
169
|
},
|
170
170
|
|
171
|
+
・
|
172
|
+
|
173
|
+
・
|
174
|
+
|
175
|
+
・
|
176
|
+
|
177
|
+
]
|
178
|
+
|
179
|
+
},
|
180
|
+
|
181
|
+
{
|
182
|
+
|
183
|
+
"id": 483,
|
184
|
+
|
185
|
+
"parent_id": 482,
|
186
|
+
|
187
|
+
"title": "フォルダ1",
|
188
|
+
|
189
|
+
"folder": true,
|
190
|
+
|
191
|
+
"bookmark": [
|
192
|
+
|
171
193
|
{
|
172
194
|
|
173
|
-
"id": 48
|
195
|
+
"id": 488,
|
174
|
-
|
196
|
+
|
175
|
-
"parent_id": 48
|
197
|
+
"parent_id": 483,
|
176
|
-
|
198
|
+
|
177
|
-
"title": "
|
199
|
+
"title": "Facebook",
|
178
200
|
|
179
201
|
"detail": "",
|
180
202
|
|
181
|
-
"reg_date": "2016/10/18 11:5
|
203
|
+
"reg_date": "2016/10/18 11:58:06",
|
182
204
|
|
183
205
|
"folder": false,
|
184
206
|
|
185
|
-
"url": "https://
|
207
|
+
"url": "https://www.facebook.com/"
|
186
208
|
|
187
209
|
}
|
188
210
|
|
@@ -190,91 +212,343 @@
|
|
190
212
|
|
191
213
|
},
|
192
214
|
|
193
|
-
|
215
|
+
・
|
216
|
+
|
194
|
-
|
217
|
+
・
|
218
|
+
|
219
|
+
・
|
220
|
+
|
221
|
+
]
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
195
|
-
|
227
|
+
```
|
196
|
-
|
228
|
+
|
229
|
+
|
230
|
+
|
197
|
-
|
231
|
+
このようなツリー構造を持つJSONとして返すというものになります.
|
232
|
+
|
198
|
-
|
233
|
+
JSONが持つIDは,DBから持ってきたIDで,Parent_IDは親ノードのIDとなります.
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
```PHP
|
240
|
+
|
199
|
-
|
241
|
+
//ブックマークをフォルダ毎に纏める
|
200
|
-
|
201
|
-
|
242
|
+
|
202
|
-
|
203
|
-
|
243
|
+
foreach ($bookmarks as $b) {
|
244
|
+
|
204
|
-
|
245
|
+
if ($tmpTags === $b['tags']) {
|
246
|
+
|
247
|
+
continue;
|
248
|
+
|
205
|
-
|
249
|
+
}
|
206
|
-
|
250
|
+
|
251
|
+
|
252
|
+
|
207
|
-
|
253
|
+
$tmpTags = $b['tags'];
|
254
|
+
|
208
|
-
|
255
|
+
$i++;
|
256
|
+
|
257
|
+
|
258
|
+
|
209
|
-
|
259
|
+
foreach ($bookmarks as $bookmark) {
|
260
|
+
|
210
|
-
|
261
|
+
if ($bookmark['tags'] === $tmpTags) {
|
262
|
+
|
211
|
-
|
263
|
+
$bookmarkItems[$i][] = $bookmark;
|
212
|
-
|
213
|
-
"detail": "",
|
214
|
-
|
215
|
-
"reg_date": "2016/10/18 11:58:06",
|
216
|
-
|
217
|
-
"folder": false,
|
218
|
-
|
219
|
-
"url": "https://www.facebook.com/"
|
220
264
|
|
221
265
|
}
|
222
266
|
|
267
|
+
}
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
//フォルダにIDを付与
|
274
|
+
|
275
|
+
foreach ($bookmarkItems as $bookmarkItem) {
|
276
|
+
|
277
|
+
$tags = explode(',', $bookmarkItem[0]['tags']);
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
foreach ($tags as $tag) {
|
282
|
+
|
283
|
+
if (!isset($tagLists[0])) {
|
284
|
+
|
285
|
+
$tagLists[] = [
|
286
|
+
|
287
|
+
'tag' => $tag,
|
288
|
+
|
289
|
+
'id' => $id,
|
290
|
+
|
223
|
-
]
|
291
|
+
];
|
292
|
+
|
224
|
-
|
293
|
+
$id++;
|
294
|
+
|
295
|
+
} else {
|
296
|
+
|
297
|
+
for ($j = 0; $j < count($tagLists); $j++) {
|
298
|
+
|
299
|
+
if ($tag === $tagLists[$j]['tag']) {
|
300
|
+
|
301
|
+
$isFind = true;
|
302
|
+
|
303
|
+
break;
|
304
|
+
|
225
|
-
}
|
305
|
+
}
|
306
|
+
|
226
|
-
|
307
|
+
$isFind = false;
|
308
|
+
|
227
|
-
|
309
|
+
}
|
310
|
+
|
311
|
+
|
312
|
+
|
228
|
-
|
313
|
+
if (!$isFind) {
|
314
|
+
|
315
|
+
$tagLists[] = [
|
316
|
+
|
317
|
+
'tag' => $tag,
|
318
|
+
|
229
|
-
|
319
|
+
'id' => $id,
|
320
|
+
|
230
|
-
|
321
|
+
];
|
322
|
+
|
323
|
+
|
324
|
+
|
231
|
-
|
325
|
+
$id++;
|
232
|
-
|
233
|
-
|
326
|
+
|
234
|
-
|
235
|
-
"folder": true,
|
236
|
-
|
237
|
-
"bookmark": [
|
238
|
-
|
239
|
-
|
327
|
+
}
|
240
|
-
|
241
|
-
"id": 489,
|
242
|
-
|
243
|
-
"parent_id": 484,
|
244
|
-
|
245
|
-
"title": "teratail【テラテイル】|思考するエンジニアのためのQAプラットフォーム",
|
246
|
-
|
247
|
-
"detail": "",
|
248
|
-
|
249
|
-
"reg_date": "2016/10/18 11:59:04",
|
250
|
-
|
251
|
-
"folder": false,
|
252
|
-
|
253
|
-
"url": "https://teratail.com/"
|
254
328
|
|
255
329
|
}
|
256
330
|
|
257
|
-
|
331
|
+
}
|
258
332
|
|
259
333
|
}
|
260
334
|
|
335
|
+
|
336
|
+
|
337
|
+
//['bookmark']に含まれるノードにParentIDを付与する
|
338
|
+
|
339
|
+
foreach ($bookmarkItems as $bookmarkItem) {
|
340
|
+
|
341
|
+
$tags = explode(',', $bookmarkItem[0]['tags']);
|
342
|
+
|
261
|
-
|
343
|
+
$end = end($tags);
|
344
|
+
|
262
|
-
|
345
|
+
foreach ($tags as $tag) {
|
346
|
+
|
347
|
+
if ($tag === $end) {
|
348
|
+
|
349
|
+
@$tagPrevValue = $tags[(count($tags) - 2)];
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
if (!is_null($tagPrevValue)) {
|
354
|
+
|
355
|
+
foreach ($tagLists as $tagList) {
|
356
|
+
|
357
|
+
if ($tagList['tag'] === $tagPrevValue) {
|
358
|
+
|
359
|
+
$tagPrevId = $tagList['id'];
|
360
|
+
|
263
|
-
|
361
|
+
}
|
362
|
+
|
264
|
-
|
363
|
+
}
|
364
|
+
|
365
|
+
|
366
|
+
|
265
|
-
|
367
|
+
for ($i = 0; $i < count($tagLists); $i++) {
|
368
|
+
|
369
|
+
if ($tagLists[$i]['tag'] === $tag) {
|
370
|
+
|
371
|
+
$tagLists[$i]['parent_id'] = $tagPrevId;
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
} else {
|
378
|
+
|
379
|
+
for ($i = 0; $i < count($tagLists); $i++) {
|
380
|
+
|
381
|
+
if ($tagLists[$i]['tag'] === $tag) {
|
382
|
+
|
383
|
+
$tagLists[$i]['parent_id'] = null;
|
384
|
+
|
385
|
+
}
|
386
|
+
|
387
|
+
}
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
}
|
392
|
+
|
393
|
+
}
|
394
|
+
|
395
|
+
}
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
//タグの空白要素を消す
|
400
|
+
|
401
|
+
foreach ($tagLists as $tagList) {
|
402
|
+
|
403
|
+
if ($tagList['tag'] === '') {
|
404
|
+
|
405
|
+
unset($tagLists[$tagList['id'] - 1]);
|
406
|
+
|
407
|
+
}
|
408
|
+
|
409
|
+
}
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
//Folder = trueをもつノードを格納する
|
414
|
+
|
415
|
+
foreach ($tagLists as $tagList) {
|
416
|
+
|
417
|
+
$tagListItems[] = [
|
418
|
+
|
419
|
+
'id' => $tagList['id'],
|
420
|
+
|
421
|
+
'parent_id' => $tagList['parent_id'],
|
422
|
+
|
423
|
+
'title' => $tagList['tag'],
|
424
|
+
|
425
|
+
'folder' => true
|
426
|
+
|
427
|
+
];
|
428
|
+
|
429
|
+
}
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
$bookmarkJson['bookmark'] = $tagListItems;
|
434
|
+
|
435
|
+
|
436
|
+
|
437
|
+
//先に入れたフォルダに対応するノードをParent_IDを付与して纏める
|
438
|
+
|
439
|
+
foreach ($bookmarkItems as $bookmarkItem) {
|
440
|
+
|
441
|
+
$tags = explode(',', $bookmarkItem[0]['tags']);
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
foreach ($tags as $tag) {
|
446
|
+
|
447
|
+
if ($tag === end($tags)) {
|
448
|
+
|
449
|
+
if ($bookmarkItem[0]['tags'] !== '') {
|
450
|
+
|
451
|
+
if (strstr($bookmarkItem[0]['tags'], $tag)) {
|
452
|
+
|
453
|
+
foreach ($bookmarkItem as $item) {
|
454
|
+
|
455
|
+
foreach ($tagLists as $tagList) {
|
456
|
+
|
457
|
+
if ($tagList['tag'] === $tag) {
|
458
|
+
|
459
|
+
$parent_id = $tagList['id'];
|
460
|
+
|
461
|
+
}
|
462
|
+
|
463
|
+
}
|
464
|
+
|
465
|
+
|
466
|
+
|
467
|
+
$bookmarkItemAfter[] = [
|
468
|
+
|
469
|
+
'id' => $id,
|
470
|
+
|
471
|
+
'parent_id' => $parent_id,
|
472
|
+
|
473
|
+
'title' => $item['title'],
|
474
|
+
|
475
|
+
'detail' => $item['note'],
|
476
|
+
|
477
|
+
'reg_date' => $item['time'],
|
478
|
+
|
479
|
+
'folder' => false,
|
480
|
+
|
481
|
+
'url' => $item['uri']
|
482
|
+
|
483
|
+
];
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
for ($i = 0; $i < count($bookmarkJson['bookmark']); $i++) {
|
488
|
+
|
489
|
+
if ($bookmarkJson['bookmark'][$i]['title'] === $tag) {
|
490
|
+
|
491
|
+
$bookmarkJson['bookmark'][$i]['bookmark'] = $bookmarkItemAfter;
|
492
|
+
|
493
|
+
$id++;
|
494
|
+
|
495
|
+
}
|
496
|
+
|
497
|
+
}
|
498
|
+
|
499
|
+
}
|
500
|
+
|
501
|
+
unset($bookmarkItemAfter);
|
502
|
+
|
503
|
+
}
|
504
|
+
|
505
|
+
} else { //tag(フォルダ)情報を持っていなかった場合にはParentIDをNULLにして格納
|
506
|
+
|
507
|
+
foreach ($bookmarkItem as $item) {
|
508
|
+
|
509
|
+
$bookmarkItemAfter = [
|
510
|
+
|
511
|
+
'id' => $id,
|
512
|
+
|
513
|
+
'parent_id' => null,
|
514
|
+
|
515
|
+
'title' => $item['title'],
|
516
|
+
|
517
|
+
'detail' => $item['note'],
|
518
|
+
|
519
|
+
'reg_date' => $item['time'],
|
520
|
+
|
521
|
+
'folder' => false,
|
522
|
+
|
523
|
+
'url' => $item['uri']
|
524
|
+
|
525
|
+
];
|
526
|
+
|
527
|
+
|
528
|
+
|
529
|
+
array_push($bookmarkJson['bookmark'], $bookmarkItemAfter);
|
530
|
+
|
531
|
+
$id++;
|
532
|
+
|
533
|
+
}
|
534
|
+
|
535
|
+
unset($bookmarkItemAfter);
|
536
|
+
|
537
|
+
}
|
538
|
+
|
539
|
+
}
|
540
|
+
|
541
|
+
}
|
542
|
+
|
543
|
+
}
|
544
|
+
|
545
|
+
return $bookmarkJson;
|
266
546
|
|
267
547
|
```
|
268
548
|
|
269
549
|
|
270
550
|
|
271
|
-
このようなツリー構造を持つJSONとして返すというものになります.
|
272
|
-
|
273
|
-
|
551
|
+
現在,上記コードで実現はできているのですが,可読性が皆無なので,スッキリさせる方法があれば教えていただけると幸いです.
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
何か良い方法があればお教えいただければ幸いです.
|
278
552
|
|
279
553
|
よろしくお願いいたします.
|
280
554
|
|