显示已验证徽章图标并显示用户列表

问题描述 投票:0回答:2
$display_name = get_the_author_meta('display_name');

$config = array(
    'premium_users' => [
        'rahat' => [
            'color' => 'blue',
            'icon' => 'fa-check-circle'
        ],
        'jondoe' => [
            'color' => 'orange',
            'icon' => 'fa-check'
        ]
    ],
    'secondary_users' => [
        'smith' => [
            'color' => 'purple',
            'icon' => 'fa-check'
        ]
    ],
);


$verified_author = "";
foreach ($config as $group => $users) {
    foreach ($users as $author => $data) {

        if ($author === $display_name && $group === 'premium_users') {
            $verified = '<'.$wrap.' class="i-'.$data['icon'].' '.
            $data['color'].'"></'.$wrap.'>';
        echo    $verified_author;
            break;
        }

    }

if($group === 'premium_users') {
    echo $group;
}

如果定义了 ['premium_users'],我想用 premium_user 回显所有用户列表。

请修改此功能。如果定义了 [premium_user],则从配置中获取值,然后回显所有拥有高级用户或二级用户的用户。

php arrays wordpress multidimensional-array author
2个回答
0
投票

您的用户信息在您的配置数组中有 2 个级别,您的 foreach 循环正在检查数组的第 1 级。将循环更改为...

foreach ($config as $group => $users) {
    foreach ($users as $user_name => $user_config) {
        if ($user_name === $display_name) {
            $verified = '<i class="'.$user_config['icon'].' '. $user_config['color'].'"></i>';
            echo $verified;
            break;
        }
    }
}

0
投票

遍历第一层的元素并使用与

isset()
相同的查找技术。

$verified = '';
foreach ($config as $level => $users) {
    if (isset($users[$display_name])) {
        $verified = sprintf(
            '<i class="%s %s"></i>',
            $users[$display_name]['icon'],
            $users[$display_name]['color']
        );
        if ($level === 'premium_users') {
            echo implode(', ', array_keys($config[$level])); // print premium names
        }                
        break;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.