Alex Budurovici

London software engineer

Sorting a multidimensional array with array_filter

array_filter is a magnificent tool, and not hard at all to be understood, and is exactly what I needed when I was doing some forensics to find out what or who triggered a chain of events that affected to appeal of QueAprendemosHoy site.

I started by querying its WordPress database, to find all the users that have a theme related permission, using the following query:

select wp_usermeta.meta_value as permissions, wp_users.user_login, wp_users.user_nicename, wp_users.user_email
from wp_usermeta
left join wp_users on wp_usermeta.user_id = wp_users.id
where wp_usermeta.meta_key = 'wp_capabilities'
and wp_usermeta.meta_value like '%theme%'

The results in an array format, were the following:

$users = array(
    array(
        'permisions'    => 'a:23:{s:9:"direccion";s:1:"1";s:16:"activate_plugins";s:1:"1";s:14:"delete_plugins";s:1:"1";s:13:"delete_themes";s:1:"1";s:14:"edit_dashboard";s:1:"1";s:10:"edit_files";s:1:"1";s:12:"edit_plugins";s:1:"1";s:18:"edit_theme_options";s:1:"1";s:11:"edit_themes";s:1:"1";s:6:"export";s:1:"1";s:6:"import";s:1:"1";s:15:"install_plugins";s:1:"1";s:14:"install_themes";s:1:"1";s:8:"level_10";s:1:"1";s:7:"level_8";s:1:"1";s:7:"level_9";s:1:"1";s:14:"manage_options";s:1:"1";s:13:"switch_themes";s:1:"1";s:17:"unfiltered_upload";s:1:"1";s:11:"update_core";s:1:"1";s:14:"update_plugins";s:1:"1";s:13:"update_themes";s:1:"1";s:12:"wpsqt-manage";s:1:"1";}',
        'user_login'    => 'user.first',
        'user_nicename' => 'user.first',
        'user_email'    => 'mail...',
    ),
    array(
        'permisions'    => 'a:28:{s:16:"activate_plugins";s:1:"1";s:14:"delete_plugins";s:1:"1";s:13:"delete_themes";s:1:"1";s:14:"edit_dashboard";s:1:"1";s:10:"edit_files";s:1:"1";s:12:"edit_plugins";s:1:"1";s:18:"edit_theme_options";s:1:"1";s:11:"edit_themes";s:1:"1";s:6:"export";s:1:"1";s:6:"import";s:1:"1";s:15:"install_plugins";s:1:"1";s:14:"install_themes";s:1:"1";s:8:"level_10";s:1:"1";s:7:"level_3";s:1:"1";s:7:"level_4";s:1:"1";s:7:"level_5";s:1:"1";s:7:"level_6";s:1:"1";s:7:"level_7";s:1:"1";s:7:"level_8";s:1:"1";s:7:"level_9";s:1:"1";s:14:"manage_options";s:1:"1";s:13:"switch_themes";s:1:"1";s:17:"unfiltered_upload";s:1:"1";s:11:"update_core";s:1:"1";s:14:"update_plugins";s:1:"1";s:13:"update_themes";s:1:"1";s:12:"wpsqt-manage";s:1:"1";s:9:"direccion";b:1;}',
        'user_login'    => 'user.second',
        'user_nicename' => 'user.second',
        'user_email'    => 'mail...',
    ),
    array(
        'permisions'    => 'a:11:{s:14:"edit_dashboard";s:1:"1";s:10:"edit_files";s:1:"1";s:18:"edit_theme_options";s:1:"1";s:11:"edit_themes";s:1:"1";s:8:"level_10";s:1:"1";s:7:"level_8";s:1:"1";s:7:"level_9";s:1:"1";s:14:"manage_options";s:1:"1";s:13:"switch_themes";s:1:"1";s:11:"update_core";s:1:"1";s:8:"redactor";b:1;}',
        'user_login'    => 'user.third',
        'user_nicename' => 'user.third',
        'user_email'    => 'mail...',
    ),
    array(
        'permisions'    => 'a:54:{s:6:"editor";b:1;s:8:"juridico";b:1;s:16:"activate_plugins";b:1;s:9:"add_users";b:1;s:12:"create_users";b:1;s:19:"delete_others_pages";b:1;s:19:"delete_others_posts";b:1;s:12:"delete_pages";b:1;s:14:"delete_plugins";b:1;s:20:"delete_private_pages";b:1;s:20:"delete_private_posts";b:1;s:22:"delete_published_pages";b:1;s:13:"delete_themes";b:1;s:12:"delete_users";b:1;s:14:"edit_dashboard";b:1;s:10:"edit_files";b:1;s:17:"edit_others_pages";b:1;s:17:"edit_others_posts";b:1;s:10:"edit_pages";b:1;s:12:"edit_plugins";b:1;s:18:"edit_private_pages";b:1;s:20:"edit_published_pages";b:1;s:18:"edit_theme_options";b:1;s:11:"edit_themes";b:1;s:10:"edit_users";b:1;s:6:"export";b:1;s:6:"import";b:1;s:15:"install_plugins";b:1;s:14:"install_themes";b:1;s:10:"list_users";b:1;s:17:"manage_categories";b:1;s:15:"manage_database";b:1;s:12:"manage_links";b:1;s:14:"manage_network";b:1;s:22:"manage_network_options";b:1;s:22:"manage_network_plugins";b:1;s:21:"manage_network_themes";b:1;s:20:"manage_network_users";b:1;s:14:"manage_options";b:1;s:17:"moderate_comments";b:1;s:13:"promote_users";b:1;s:13:"publish_pages";b:1;s:13:"publish_posts";b:1;s:18:"read_private_pages";b:1;s:18:"read_private_posts";b:1;s:12:"remove_users";b:1;s:13:"switch_themes";b:1;s:15:"unfiltered_html";b:1;s:17:"unfiltered_upload";b:1;s:11:"update_core";b:1;s:14:"update_plugins";b:1;s:13:"update_themes";b:1;s:10:"view_stats";b:1;s:12:"wpsqt-manage";b:1;}',
        'user_login'    => 'user.fourth',
        'user_nicename' => 'user.fourth',
        'user_email'    => 'mail...',
    ),
    array(
        'permisions'    => 'a:6:{s:10:"edit_files";s:1:"1";s:18:"edit_theme_options";s:1:"1";s:8:"level_10";s:1:"1";s:7:"level_8";s:1:"1";s:7:"level_9";s:1:"1";s:12:"comunicacion";s:1:"1";}',
        'user_login'    => 'user.fifth',
        'user_nicename' => 'user.fifth',
        'user_email'    => 'mail...',
    ),
    array(
        'permisions'    => 'a:24:{s:14:"_debug_objects";s:1:"1";s:16:"activate_plugins";s:1:"1";s:14:"delete_plugins";s:1:"1";s:13:"delete_themes";s:1:"1";s:14:"edit_dashboard";s:1:"1";s:10:"edit_files";s:1:"1";s:12:"edit_plugins";s:1:"1";s:18:"edit_theme_options";s:1:"1";s:11:"edit_themes";s:1:"1";s:6:"export";s:1:"1";s:6:"import";s:1:"1";s:15:"install_plugins";s:1:"1";s:14:"install_themes";s:1:"1";s:8:"level_10";s:1:"1";s:7:"level_8";s:1:"1";s:7:"level_9";s:1:"1";s:14:"manage_options";s:1:"1";s:13:"switch_themes";s:1:"1";s:17:"unfiltered_upload";s:1:"1";s:11:"update_core";s:1:"1";s:14:"update_plugins";s:1:"1";s:13:"update_themes";s:1:"1";s:12:"wpsqt-manage";s:1:"1";s:8:"redactor";b:1;}',
        'user_login'    => 'user.sixth',
        'user_nicename' => 'user.sixth',
        'user_email'    => 'mail...',
    ),
    array(
        'permisions'    => 'a:6:{s:14:"manage_network";b:1;s:22:"manage_network_options";b:1;s:22:"manage_network_plugins";b:1;s:21:"manage_network_themes";b:1;s:20:"manage_network_users";b:1;s:10:"tecnologia";b:1;}',
        'user_login'    => 'user.seventh',
        'user_nicename' => 'user.seventh',
        'user_email'    => 'mail...',
    ),
    array(
        'permisions'    => 'a:51:{s:6:"editor";b:1;s:8:"juridico";b:1;s:16:"activate_plugins";b:1;s:9:"add_users";b:1;s:12:"create_users";b:1;s:19:"delete_others_pages";b:1;s:19:"delete_others_posts";b:1;s:12:"delete_pages";b:1;s:14:"delete_plugins";b:1;s:20:"delete_private_pages";b:1;s:20:"delete_private_posts";b:1;s:22:"delete_published_pages";b:1;s:22:"delete_published_posts";b:1;s:13:"delete_themes";b:1;s:12:"delete_users";b:1;s:14:"edit_dashboard";b:1;s:17:"edit_others_pages";b:1;s:17:"edit_others_posts";b:1;s:10:"edit_pages";b:1;s:12:"edit_plugins";b:1;s:18:"edit_private_pages";b:1;s:18:"edit_private_posts";b:1;s:20:"edit_published_pages";b:1;s:20:"edit_published_posts";b:1;s:18:"edit_theme_options";b:1;s:11:"edit_themes";b:1;s:10:"edit_users";b:1;s:6:"export";b:1;s:6:"import";b:1;s:15:"install_plugins";b:1;s:14:"install_themes";b:1;s:10:"list_users";b:1;s:17:"manage_categories";b:1;s:12:"manage_links";b:1;s:22:"manage_network_options";b:1;s:22:"manage_network_plugins";b:1;s:21:"manage_network_themes";b:1;s:20:"manage_network_users";b:1;s:14:"manage_options";b:1;s:17:"moderate_comments";b:1;s:13:"promote_users";b:1;s:13:"publish_pages";b:1;s:13:"publish_posts";b:1;s:18:"read_private_pages";b:1;s:18:"read_private_posts";b:1;s:12:"remove_users";b:1;s:13:"switch_themes";b:1;s:17:"unfiltered_upload";b:1;s:11:"update_core";b:1;s:14:"update_plugins";b:1;s:13:"update_themes";b:1;}',
        'user_login'    => 'user.eighth',
        'user_nicename' => 'user.eighth',
        'user_email'    => 'mail...',
    ),
);

In order to only show the theme related permissions, and removing all others, array_filter is what I needed to use:

echo "<pre>";
foreach ($users as $key => $value) {
    $permisions = unserialize($value['permisions']);
    $value['permisions'] = array_filter(array_keys($permisions), function ($k) {
        return strpos($k, 'theme');
    });

    var_dump($value);
    echo "<hr />";
}

As I’m iterating through the array,

  • for each of its index, I’m de-serializing the permissions key’s value into $permissions
  • then array_filter comes into play, running through the keys of the unserialized $permissions array and in the callback function, having a strpos to return just the keys that contain theme keyword.
  • Voila!

Give it a try, and see for yourself.

array_filtermysqlphpwordpress

Alexandru Budurovici • October 18, 2014


Previous Post

Next Post