Fork Copy each(function($field) use (&$result) { $arr = explode('.', $field); if (count($arr) === 1) { $result[] = $field; } else { $last = array_first(array_splice($arr, count($arr) - 1, 1)); $key = join('.', $arr); if ($last == '*') { data_set($result, $key, $last); } elseif (data_get($result, $key) != '*') { $currentValue = data_get($result, $key, []); $currentValue[] = $last; data_set($result, $key, $currentValue); } } }); var_dump($result);