多维数组 - uasort无效,usort删除键

时间:2014-04-29 03:42:12

标签: php arrays sorting multidimensional-array

一种复杂的分类要求。 数据如下所示:

array (size=238)
527 => 
array (size=2)
  0 => 
    array (size=5)
      'person_name' => string 'Ms. Sandra Stokes I' (length=19)
      'archive_id' => int 1
      'archive_item_id' => int 1
      'meta_data_id' => int 28151
      'person_id' => int 527
  1 => 
    array (size=5)
      'person_name' => string 'Ms. Sandra Stokes I' (length=19)
      'archive_id' => int 1
      'archive_item_id' => int 22
      'meta_data_id' => int 28418
      'person_id' => int 527
766 => 
array (size=6)
  0 => 
    array (size=5)
      'person_name' => string 'Rhett Eichmann' (length=14)
      'archive_id' => int 1
      'archive_item_id' => int 1
      'meta_data_id' => int 28251
      'person_id' => int 766
  1 => 
    array (size=5)
      'person_name' => string 'Rhett Eichmann' (length=14)
      'archive_id' => int 1
      'archive_item_id' => int 10
      'meta_data_id' => int 28272
      'person_id' => int 766
  2 => 
    array (size=5)
      'person_name' => string 'Rhett Eichmann' (length=14)
      'archive_id' => int 1
      'archive_item_id' => int 19
      'meta_data_id' => int 28378
      'person_id' => int 766
  3 => 
    array (size=5)
      'person_name' => string 'Rhett Eichmann' (length=14)
      'archive_id' => int 1
      'archive_item_id' => int 20
      'meta_data_id' => int 28213
      'person_id' => int 766
  4 => 
    array (size=5)
      'person_name' => string 'Rhett Eichmann' (length=14)
      'archive_id' => int 1
      'archive_item_id' => int 22
      'meta_data_id' => int 28869
      'person_id' => int 766
  5 => 
    array (size=5)
      'person_name' => string 'Rhett Eichmann' (length=14)
      'archive_id' => int 1
      'archive_item_id' => int 28
      'meta_data_id' => int 28135
      'person_id' => int 766

使用的比较函数:

function cmp($a, $b)
        {
            if ($a[0]['person_name'] == $b[0]['person_name'])
            {
                return 0;
            }
            return ($a[0]['person_name'] < $b[0]['person_name']) ? -1 : 1;
        }            

        usort($data, "cmp");

使用usort,返回按人名的字母顺序排列, 像这样,但剥离了id索引:

"person_ids": [
    [
        {
            "person_name": "Abner Becker",
            "archive_id": 1,
            "archive_item_id": 2,
            "meta_data_id": 28376,
            "person_id": 538
        }
    ],
    [
        {
            "person_name": "Addie Legros",
            "archive_id": 1,
            "archive_item_id": 8,
            "meta_data_id": 28365,
            "person_id": 606
        },
        {
            "person_name": "Addie Legros",
            "archive_id": 1,
            "archive_item_id": 20,
            "meta_data_id": 28594,
            "person_id": 606
        },
        {
            "person_name": "Addie Legros",
            "archive_id": 1,
            "archive_item_id": 21,
            "meta_data_id": 28405,
            "person_id": 606
        }
    ],

使用uasort,键被保留但似乎没有调用该函数。

{
"person_ids": {
    "502": [
        {
            "person_name": "Mr. Tara Bartoletti",
            "archive_id": 1,
            "archive_item_id": 15,
            "meta_data_id": 28247,
            "person_id": 502
        }
    ],
    "503": [
        {
            "person_name": "Stacy Adams",
            "archive_id": 1,
            "archive_item_id": 8,
            "meta_data_id": 28918,
            "person_id": 503
        },
        {
            "person_name": "Stacy Adams",
            "archive_id": 1,
            "archive_item_id": 22,
            "meta_data_id": 28682,
            "person_id": 503
        },
        {
            "person_name": "Stacy Adams",
            "archive_id": 1,
            "archive_item_id": 26,
            "meta_data_id": 28148,
            "person_id": 503
        }
    ],
    "504": [

关于如何在保持id键的同时通过person_name键完成排序的任何建议?

2 个答案:

答案 0 :(得分:1)

您应该按如下方式对子数组进行排序:

foreach ($array as $id => &$sub_array) {
  usort($sub_array, function($a, $b) {
    return strcmp($a['person_name'], $b['person_name']);
  });
}
var_dump($array);

答案 1 :(得分:1)

哇 - 罪魁祸首是在Postman&#34;漂亮&#34;查看,它在查看JSON时重新排序所有键!这是一个有趣的2小时圈。