以下是一个PHP递归函数的实例,该函数用于查找给定用户在组织结构中的上级。
```php

// 假设我们有一个用户数组,其中包含用户ID、用户名和上级ID
$users = [
['id' => 1, 'username' => 'John Doe', 'parent_id' => null],
['id' => 2, 'username' => 'Jane Doe', 'parent_id' => 1],
['id' => 3, 'username' => 'Alice Smith', 'parent_id' => 2],
['id' => 4, 'username' => 'Bob Johnson', 'parent_id' => 3],
['id' => 5, 'username' => 'Charlie Brown', 'parent_id' => 3],
['id' => 6, 'username' => 'Diana Prince', 'parent_id' => 5],
];
// 递归函数,用于查找用户的上级
function findParent($userId, $users) {
foreach ($users as $user) {
if ($user['id'] == $userId) {
return $user['parent_id'] !== null ? findParent($user['parent_id'], $users) : null;
}
}
return null;
}
// 测试函数
echo "









