Wordpress PHP array filter out subscribers

admin

Administrator
Staff member
I am working on a wordpress dashboard plugin that is designed to display a list of subscribers in the admin dashboard in a wordpress table.

Everything works correctly apart from the fact that all registered users are displayed in the array when I need just subscribers. I am creating the array with the following code..

Code:
function prepare_items() {
    $per_page = 5;
    $columns = $this->get_columns();        $hidden = array();        $sortable = $this->get_sortable_columns();
    $this->_column_headers = array($columns, $hidden, $sortable);

    global $wpdb;
    $data = $wpdb->get_results("SELECT * FROM $wpdb->users", ARRAY_A);  
    $i = 0;

    foreach($data as $single) {
    $meta = $wpdb->get_results(
    "SELECT meta_value
    FROM $wpdb->usermeta
    WHERE user_id = $single[ID]
    AND (meta_key = 'first_name' OR meta_key = 'last_name' OR meta_key = 'wp_user_level')
    ORDER BY meta_key",
    ARRAY_A
    );

    $data[$i]['first_name'] = $meta[0]['meta_value'];
    $data[$i]['last_name'] = $meta[1]['meta_value'];
    $data[$i]['wp_user_level'] = $meta[2]['meta_value'];
    $i++;
}

    echo '<pre>'; 
    print_r($data2); 
    echo '</pre>';

By using print_r I can see that the two tables wp_users and wp_usermeta are being correctly joined and all of the details I need are returned into the array.

What I now need to do is use some kind of array_filter to only return entries that have a wp_user_level of 0 (subscriber).

I have tried the following....

Code:
$data = array_filter($data, 'filter_subscribers');

function filter_subscribers()
    {
        return $data2['wp_user_level'] == '0';
    }

But it is not working for me, can anyone suggest anything?