How can PHP be used to display a hierarchical referral structure with multiple levels?

To display a hierarchical referral structure with multiple levels in PHP, you can use a recursive function to traverse through the levels of the structure and display each level accordingly. This function should check if a member has any referrals and if so, recursively call itself to display the referrals of that member.

function displayReferrals($member, $level = 0) {
    echo str_repeat('-', $level) . $member['name'] . "<br>";
    
    if(isset($member['referrals'])) {
        foreach($member['referrals'] as $referral) {
            displayReferrals($referral, $level + 1);
        }
    }
}

// Example hierarchical referral structure
$referralStructure = [
    'name' => 'A',
    'referrals' => [
        [
            'name' => 'B',
            'referrals' => [
                [
                    'name' => 'C',
                    'referrals' => []
                ],
                [
                    'name' => 'D',
                    'referrals' => []
                ]
            ]
        ],
        [
            'name' => 'E',
            'referrals' => []
        ]
    ]
];

// Display the hierarchical referral structure
displayReferrals($referralStructure);