How to sort a 3D array containing objects?

I am creating a WordPress site where I need to sort the WP_User_Query results AFTER the query has already been run. For those of you unfamiliar, it has elements that look like this:

Array (
  [0] => WP_User Object (
    [data] => stdClass Object (
      [ID] => 1
      [user_login] => MarvinLazer
      [user_pass] => $P$BUGHRCjMzlvn7dlGp53UTPC8GMF081/
      [user_nicename] => marvinlazer
      [user_email] => marvin@lazer.com
      [user_url] => http://marvinlazer.com
      [user_registered] => 2017-03-04 23:08:08
      [user_activation_key] =>
      [user_status] => 0
      [display_name] => Marvin Lazer
    )
    [ID] => 1
    [caps] => Array (
      [subscriber] => 1
    )
    [cap_key] => wp_capabilities
    [roles] => Array (
      [0] => subscriber
    )
    [allcaps] => Array (
      [read] => 1
      [level_0] => 1
      [subscriber] => 1
    )
    [filter] =>
  )
  [1] => WP_User Object ( etc. etc.

      

Based on this very helpful page on PHP array sorting , I feel like I have something close. Unfortunately this just gives me a blank page after the part where the code appears.

          function cmp(array $a, array $b) {
              if ($a['data']['display_name'] < $b['data']['display_name']) {
                  return -1;
              } else if ($a['data']['display_name'] > $b['data']['display_name']) {
                  return 1;
              } else {
                  return 0;
              }
          }

          usort($user_query->results, 'cmp');

      

Does anyone want to point me to what I am doing wrong?

+3


source to share


1 answer


This is because you are trying to access a property of an object in an array element accessor. You cannot use an index to access an object object. You should use ->

instead []

. You can check this demo to show how to access the object.

refer to how to access an array / object



change yours $a['data']['display_name']

to $a->data->display_name

and yours $b

in the compare function.

+1


source







All Articles