Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $entries = [
- 1 => [
- 'value' => 'entry 1',
- ],
- 2 => [
- 'value' => 'entry 2',
- ],
- ];
- $subEntries = [
- 'a' => [
- 'foo' => 'barA',
- 'parent_entry' => 1,
- ],
- 'b' => [
- 'foo' => 'barB',
- 'parent_entry' => 1,
- ],
- 'c' => [
- 'foo' => 'barC',
- 'parent_entry' => 2,
- ],
- ];
- $subEntriesPerParentEntry = [];
- foreach ($subEntries as $key => $value) {
- $parentEntryId = $value['parent_entry'];
- $subEntriesPerParentEntry[$parentEntryId][$key] = $value;
- }
- $tree = [];
- foreach ($entries as $key => $value) {
- $value['childs'] = $subEntriesPerParentEntry[$key] ?? [];
- $tree[$key] = $value;
- }
- echo json_encode($tree, JSON_PRETTY_PRINT);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement