-1

I got this array:

Array
(
    [0] => Array
        (
            [_complex] => 1
            [_attributes] => Array
                (
                    [id] => 453
                )

            [_data] => Array
                (
                    [id] => 453
                    [nid] => lastname
                 )
         )
   [1] => Array
        (
            [_complex] => 1
            [_attributes] => Array
                (
                    [id] => 455
                )

            [_data] => Array
                (
                    [id] => 455
                    [nid] => lastname
                 )
         )

How to sort it by [_data][nid]?

user2213609
  • 73
  • 1
  • 8

2 Answers2

1
 uasort(
      $array,
      function ($a,$b) {
           return strcmp($a['_data']['nid'],$b['_data']['nid']);
      }
 );

php.net: uasort()

katharas
  • 381
  • 1
  • 6
0

Is the ID always the same in an array? Then why you don't convert it something like this:

Array
(
    [0] => Array
        (
            [_complex] => 1
            [id] => 453
            [nid] => lastname
        )

    [1] => Array
        (
            [_complex] => 1
            [id] => 455
            [nid] => lastname
        )
)

Or is there a reason why not? This would be easier to sort and more comfortable to handle. When you convert it you could use this function:

aasort($your_Array, "nid");

function aasort (&$array, $key) {
        $sorter=array();
        $ret=array();
        reset($array);

        foreach ($array as $ii => $va) {
            $sorter[$ii]=$va[$key];
        }
        asort($sorter);

        foreach ($sorter as $ii => $va) {
            $ret[$ii]=$array[$ii];
        }

        $array=$ret;
    }
Zaziki
  • 388
  • 1
  • 12