Revision: 65622
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at January 3, 2014 02:27 by CreativePunch
Initial Code
function makeNested($source) { $nested = array(); foreach ( $source as &$s ) { if ( is_null($s['parent_id']) ) { // no parent_id so we put it in the root of the array $nested[] = &$s; } else { $pid = $s['parent_id']; if ( isset($source[$pid]) ) { // If the parent ID exists in the source array // we add it to the 'children' array of the parent after initializing it. if ( !isset($source[$pid]['children']) ) { $source[$pid]['children'] = array(); } $source[$pid]['children'][] = &$s; } } } return $nested; }
Initial URL
http://creative-punch.net/2014/01/creating-nested-array-items-parent-ids/
Initial Description
Creating a nested array from items with parent IDs. This is useful for when you have a website working with multiple categories with parent categories. But it can be applied anywhere, really!
Initial Title
Creating a nested array from items with parent IDs
Initial Tags
php, sort, array
Initial Language
PHP