Skip to content

Instantly share code, notes, and snippets.

@dwihujianto
Created November 19, 2019 06:31
Show Gist options
  • Save dwihujianto/16eae8c1bdc53ccabe171e2eac7e86b4 to your computer and use it in GitHub Desktop.
Save dwihujianto/16eae8c1bdc53ccabe171e2eac7e86b4 to your computer and use it in GitHub Desktop.
<?php
function transform_tree($categories) {
$data = [];
foreach($categories as $key => $category) {
$data[$key] = [
'name' = $category->name
];
if ($category->children) {
$data[$key]['children'] = transform_tree($category->children);
}
if ($category->audio) {
$data[$key]['downloadable'] = true;
$data[$key]['audio'] = $category->audio;
}
}
return $data;
}
// build
$trees = build_tree($categories);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment