1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| function getUserStr($arr) {
function getList($arr, &$data=[], $key='') { if ($key && !array_key_exists($key, $data)) $data[$key] = []; if (is_array($arr)) foreach ($arr as $k => $v) { if ($key) $data[$key][] = $k; getList($v, $data, $k); } return $data; } $list = getList($arr);
function getUserSubordinate($list, &$res, $pk, $ck) { $res[$pk] = array_merge($res[$pk], $list[$ck]); foreach ($list[$ck] as $key => $value) { if (array_key_exists($value, $list) && count($list[$value])) { getUserSubordinate($list, $res, $pk, $value); } } } $res = []; foreach ($list as $key => $value) { $res[$key] = $value; foreach ($value as $ck => $cv) { if (array_key_exists($cv, $list) && count($list[$cv])) { getUserSubordinate($list, $res, $key, $cv); } } }
$str = ''; foreach ($res as $key => $value) { $str .= $key . ': ' . ($value ? implode(',', $value) : '无') . '<br>'; } return $str; }
|