+include_once __DIR__ . '/includes/common.inc';
+function getdatafromfile($file_name) {
+ $csv = array_map('str_getcsv', file($file_name));
+ array_walk($csv, function (&$a) use ($csv) {
+ $a = (object) array_combine($csv[0], $a);
+$rows = getdatafromfile('acs_census_table_number_lookup1.csv');
+// Define maine variables to walk through flat array.
+// Run data row processing of flat array.
+foreach ($rows as $row) {
+ // Detect current indent.
+ $current_indent = substr_count($row->hierarchical_labels, $indentation);
+ if ($current_indent == 0) {
+ // We should always use `table_id` as key on the root level.
+ $parents = array($row->table_id);
+ elseif ($current_indent > $previous_indent) {
+ $leaf = drupal_array_get_nested_value($tree, $parents);
+ array_push($parents, key($leaf), 'child');
+ elseif ($current_indent < $previous_indent) {
+ $parents = array_slice($parents, 0, -2);
+ elseif ($current_indent == $previous_indent) {
+ // Get items by cursor.
+ $leaf = drupal_array_get_nested_value($tree, $parents);
+ // Add to items a new one by cursor.
+ $leaf[$row->hierarchical_labels] = array(
+ // Update items by cursor.
+ drupal_array_set_nested_value($tree, $parents, $leaf);
+ // Update previous indent.
+ $previous_indent = $current_indent;