Skip to content

Instantly share code, notes, and snippets.

@ym
Created December 17, 2012 14: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 ym/e4cf4feea0faeddc2c06 to your computer and use it in GitHub Desktop.
Save ym/e4cf4feea0faeddc2c06 to your computer and use it in GitHub Desktop.
<?php
$result = [];
foreach(db::test()->query('SELECT `id`,`title`, `parent_id` FROM `table` GROUP BY `parent_id` ORDER BY `parent_id` ASC')->fetchAll() as $item) {
if($item['parent_id'] == 0) {
if(!isset($result[$item['id']])) {
$result[$item['id']] = $item;
}
} else {
if(!isset($result[$item['id']]['child'])) $result[$item['id']]['child'] = [];
$result[$item['id']]['child'][] = $item;
}
}
echo json_encode(array_values($result));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment