-1

我得到了这个数组:

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
                 )
         )

如何排序[_data][nid]

4

2 回答 2

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

php.net: uasort()

于 2013-04-23T07:08:39.000 回答
0

数组中的 ID 是否始终相同?那你为什么不把它转换成这样:

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

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

或者有什么理由不这样做?这将更容易分类并且处理起来更舒适。当你转换它时,你可以使用这个功能:

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;
    }
于 2013-04-23T07:09:17.580 回答