使用PHP Foreach循环外部的变量

时间:2017-05-07 15:44:02

标签: arrays wordpress email

我正在尝试在foreach循环之外输出用户名和用户电子邮件。我正在尝试向所有属于某个用户角色的WordPress用户发送电子邮件。

这是我的代码:

// Get users and their roles
    $user_args = array(
      'role__in' => 'new_role', 
      'orderby'  => 'user_nicename',
      'order'    => 'ASC'
    );  
    $users = get_users($user_args); 

    foreach ( $users as $user ) :       
        $user_name  = $user->user_email;
        $user_email = $user->display_name;          
    endforeach;

然后我可以在页面的其他区域使用它们,即:。

echo 'Hello, ' . $user_name;
echo 'Send to: ' .$user_email;

我打印出$ users数组,其中包含以下内容:

Array
(
    [0] => WP_User Object
        (
            [data] => stdClass Object
                (
                    [ID] => 46
                    [user_login] => huw
                    [user_pass] => u7CqxbuQDvApUAF6tT.
                    [user_nicename] => huw
                    [user_email] => huw@xxxx.co.uk
                    [user_url] => 
                    [user_registered] => 2017-02-06 11:13:09
                    [user_activation_key] => 1486379590:$P$BkisA4T5j1S/ZjRageafNYHfsdin1S0
                    [user_status] => 0
                    [display_name] => Huw Daniel Rowlands
                )

            [ID] => 46
            [caps] => Array
                (
                    [sssg] => 1
                    [new_role] => 1
                    [site_member] => 1
                    [test_role] => 1
                )

            [cap_key] => jciw_capabilities
            [roles] => Array
                (
                    [0] => sssg
                    [1] => new_role
                    [2] => site_member
                    [3] => test_role
                )

            [allcaps] => Array
                (
                    [read] => 1
                    [sssg] => 1
                    [new_role] => 1
                    [site_member] => 1
                    [test_role] => 1
                )

            [filter] => 
        )

    [1] => WP_User Object
        (
            [data] => stdClass Object
                (
                    [ID] => 308
                    [user_login] => jeremy
                    [user_pass] => LLOKbkPOWQsBKUIk2qL1
                    [user_nicename] => magnus
                    [user_email] => jeremy@gmail.com
                    [user_url] => 
                    [user_registered] => 2017-05-03 19:24:42
                    [user_activation_key] => 1493839482:$P$BI/IYldCzsXZowLEiNfxiUkIwVdDKV0
                    [user_status] => 0
                    [display_name] => Jeremy
                )

            [ID] => 308
            [caps] => Array
                (
                    [new_role] => 1
                    [sssg] => 1
                    [site_member] => 1
                )

            [cap_key] => jciw_capabilities
            [roles] => Array
                (
                    [0] => new_role
                    [1] => sssg
                    [2] => site_member
                )

            [allcaps] => Array
                (
                    [read] => 1
                    [new_role] => 1
                    [sssg] => 1
                    [site_member] => 1
                )

            [filter] => 
        )

)

0 个答案:

没有答案