按两个给定键对多维数组进行排序

我有一个看起来像这样的数组:


[

{

    "id": "1",

    "country_id": "1",

    "spec_id": "1",

    "spec_children_name": "SUPER REDUCES RATE",

    "spec_children_first_col": "",

    "spec_children_second_col": "",

    "spec_children_third_col": ""

},

{

    "id": "2",

    "country_id": "1",

    "spec_id": "1",

    "spec_children_name": "REDUCED RATE",

    "spec_children_first_col": "10% and 13%",

    "spec_children_second_col": "food, passenger transport, accommodotion, newspaper, pharmaceutical products,\u2026.(10%); plants, antiques, firewood, cinema, theatre,\u2026(13%)",

    "spec_children_third_col": ""

},

{

    "id": "3",

    "country_id": "1",

    "spec_id": "1",

    "spec_children_name": "MEDIUM RATE",

    "spec_children_first_col": "",

    "spec_children_second_col": "",

    "spec_children_third_col": ""

},

{

    "id": "4",

    "country_id": "1",

    "spec_id": "1",

    "spec_children_name": "STANDARD RATE",

    "spec_children_first_col": "20%",

    "spec_children_second_col": "other",

    "spec_children_third_col": ""

},

{

    "id": "5",

    "country_id": "1",

    "spec_id": "1",

    "spec_children_name": "ZERO RATE",

    "spec_children_first_col": "",

    "spec_children_second_col": "",

    "spec_children_third_col": ""

},

    {

        "id": "104",

        "country_id": "2",

        "spec_id": "1",

        "spec_children_name": "REDUCED RATE",

        "spec_children_first_col": "TEXT 547",

        "spec_children_second_col": "TEXT 1000",

        "spec_children_third_col": ""

    }

]

我想要什么:我想按2个对象键比较对此数组进行排序,如果和.最后,它应该看起来像这样:spec_children_namespec_id


    [

        [

    {

        "id": "1",

        "country_id": "1",

        "spec_id": "1",

        "spec_children_name": "SUPER REDUCES RATE",

        "spec_children_first_col": "",

        "spec_children_second_col": "",

        "spec_children_third_col": ""

    },


墨色风雨
浏览 113回答 3
3回答

小唯快跑啊

您的问题基本上是如何根据两个键对数组进行排序。在许多编程语言中,包括PHP,答案是创建一个比较函数(有时称为比较器),该函数根据其参数所需的相对顺序返回值(通常为-1,0,1)。如果两个键中的第一个参数不同,则根据第一个键的顺序返回 -1 或 1。它们在第一个键上相等,检查第二个键 - 并根据第二个键的顺序返回-1,0,1。此逻辑自然可以扩展到任意数量的密钥。PHP Spaceship 运算符&nbsp;<=>&nbsp;为许多内置类型实现了此逻辑。下面的代码演示了使用 PHP 的方案的这一点。它是从这个问题改编而来的。在这里,我假设您要按速率(零<超减<减<中等<标准)进行排序,然后按(数字)进行排序。我也纠正了.spec_idSUPER REDUCES RATESUPER REDUCED RATE$RATE_ORDERING = array(&nbsp; 'ZERO RATE' => 1,&nbsp; 'SUPER REDUCED RATE' => 2,&nbsp;&nbsp; 'REDUCED RATE' => 3,&nbsp; 'MEDIUM RATE' => 4,&nbsp; 'STANDARD RATE' => 5&nbsp; );$arr = array(&nbsp; &nbsp; array('spec_id' => 2, 'spec_children_name' => 'STANDARD RATE'),&nbsp; &nbsp; array('spec_id' => 1, 'spec_children_name' => 'STANDARD RATE'),&nbsp; &nbsp; array('spec_id' => 2, 'spec_children_name' => 'ZERO RATE'),&nbsp; &nbsp; array('spec_id' => 1, 'spec_children_name' => 'MEDIUM RATE'),&nbsp; &nbsp; array('spec_id' => 2, 'spec_children_name' => 'REDUCED RATE'),&nbsp; &nbsp; array('spec_id' => 2, 'spec_children_name' => 'MEDIUM RATE'),&nbsp; &nbsp; array('spec_id' => 1, 'spec_children_name' => 'SUPER REDUCED RATE'),&nbsp; &nbsp; array('spec_id' => 2, 'spec_children_name' => 'SUPER REDUCED RATE'),&nbsp; &nbsp; array('spec_id' => 1, 'spec_children_name' => 'REDUCED RATE'),&nbsp; &nbsp; array('spec_id' => 1, 'spec_children_name' => 'ZERO RATE'));usort($arr, function ($a, $b) use ($RATE_ORDERING) {&nbsp; $result = $RATE_ORDERING[$a['spec_children_name']] - $RATE_ORDERING[$b['spec_children_name']];&nbsp; if ($result != 0) {&nbsp; &nbsp; return $result;&nbsp; }&nbsp; return $a['spec_id'] - $b['spec_id'];});var_dump($arr);输出:array(10) {&nbsp; [0]=>&nbsp; array(2) {&nbsp; &nbsp; ["spec_id"]=>&nbsp; &nbsp; int(1)&nbsp; &nbsp; ["spec_children_name"]=>&nbsp; &nbsp; string(9) "ZERO RATE"&nbsp; }&nbsp; [1]=>&nbsp; array(2) {&nbsp; &nbsp; ["spec_id"]=>&nbsp; &nbsp; int(2)&nbsp; &nbsp; ["spec_children_name"]=>&nbsp; &nbsp; string(9) "ZERO RATE"&nbsp; }&nbsp; [2]=>&nbsp; array(2) {&nbsp; &nbsp; ["spec_id"]=>&nbsp; &nbsp; int(1)&nbsp; &nbsp; ["spec_children_name"]=>&nbsp; &nbsp; string(18) "SUPER REDUCED RATE"&nbsp; }&nbsp; [3]=>&nbsp; array(2) {&nbsp; &nbsp; ["spec_id"]=>&nbsp; &nbsp; int(2)&nbsp; &nbsp; ["spec_children_name"]=>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[17/133]&nbsp; &nbsp; string(18) "SUPER REDUCED RATE"&nbsp; }&nbsp; [4]=>&nbsp; array(2) {&nbsp; &nbsp; ["spec_id"]=>&nbsp; &nbsp; int(1)&nbsp; &nbsp; ["spec_children_name"]=>&nbsp; &nbsp; string(12) "REDUCED RATE"&nbsp; }&nbsp; [5]=>&nbsp; array(2) {&nbsp; &nbsp; ["spec_id"]=>&nbsp; &nbsp; int(2)&nbsp; &nbsp; ["spec_children_name"]=>&nbsp; &nbsp; string(12) "REDUCED RATE"&nbsp; }&nbsp; [6]=>&nbsp; array(2) {&nbsp; &nbsp; ["spec_id"]=>&nbsp; &nbsp; int(1)&nbsp; &nbsp; ["spec_children_name"]=>&nbsp; &nbsp; string(11) "MEDIUM RATE"&nbsp; }&nbsp; [7]=>&nbsp; array(2) {&nbsp; &nbsp; ["spec_id"]=>&nbsp; &nbsp; int(2)&nbsp; &nbsp; ["spec_children_name"]=>&nbsp; &nbsp; string(11) "MEDIUM RATE"&nbsp; }&nbsp; [8]=>&nbsp; array(2) {&nbsp; &nbsp; ["spec_id"]=>&nbsp; &nbsp; int(1)&nbsp; &nbsp; ["spec_children_name"]=>&nbsp; &nbsp; string(13) "STANDARD RATE"&nbsp; }&nbsp; [9]=>&nbsp; array(2) {&nbsp; &nbsp; ["spec_id"]=>&nbsp; &nbsp; int(2)&nbsp; &nbsp; ["spec_children_name"]=>&nbsp; &nbsp; string(13) "STANDARD RATE"&nbsp; }}

拉风的咖菲猫

宇宙飞船操作员将自动处理所有数据类型。将动态列名和排序方向“factor”传递到自定义函数作用域中。use()代码:$objects = json_decode($json);$column = 'spec_children_name';$direction = 'asc';$reverser = $direction === 'asc' ? 1 : -1;uasort($objects, function($a, $b) use ($column, $reverser) {&nbsp; &nbsp; return $reverser * ($a->$column <=> $b->$column);});var_export($objects);或者也许...$objects = json_decode($json);$rules = ['spec_id' => 'ASC', 'spec_children_name' => 'DESC'];uasort($objects, function($a, $b) use ($rules) {&nbsp; &nbsp; foreach ($rules as $column => $order) {&nbsp; &nbsp; &nbsp; &nbsp; $left[] = $order === 'ASC' ? $a->$column : $b->$column;&nbsp; &nbsp; &nbsp; &nbsp; $right[] = $order === 'ASC' ? $b->$column : $a->$column;&nbsp; &nbsp; }&nbsp; &nbsp; return $left <=> $right;});var_export($objects);

慕村9548890

几乎没有理由像array_multisort和array_column那样重新发明一个功能。$data =[&nbsp; &nbsp; [&nbsp; &nbsp; 'name' => 'John',&nbsp; &nbsp; 'age'&nbsp; => 34&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; 'name'&nbsp; => 'Jack',&nbsp; &nbsp; 'age'&nbsp; &nbsp;=> 55,&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; 'name' => 'Adam',&nbsp; &nbsp; 'age'&nbsp; => 42&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; 'name' => 'Jack',&nbsp; &nbsp; 'age'&nbsp; => 78&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; 'name' => 'Adam',&nbsp; &nbsp; 'age'&nbsp; => 80&nbsp; &nbsp; ]];array_multisort(array_column($data, 'name'), SORT_ASC, array_column($data, 'age'), SORT_DESC, $data);var_export($data);输出:array (&nbsp; 0 =>&nbsp;&nbsp; array (&nbsp; &nbsp; 'name' => 'Adam',&nbsp; &nbsp; 'age' => 80,&nbsp; ),&nbsp; 1 =>&nbsp;&nbsp; array (&nbsp; &nbsp; 'name' => 'Adam',&nbsp; &nbsp; 'age' => 42,&nbsp; ),&nbsp; 2 =>&nbsp;&nbsp; array (&nbsp; &nbsp; 'name' => 'Jack',&nbsp; &nbsp; 'age' => 78,&nbsp; ),&nbsp; 3 =>&nbsp;&nbsp; array (&nbsp; &nbsp; 'name' => 'Jack',&nbsp; &nbsp; 'age' => 55,&nbsp; ),&nbsp; 4 =>&nbsp;&nbsp; array (&nbsp; &nbsp; 'name' => 'John',&nbsp; &nbsp; 'age' => 34,&nbsp; ),)
打开App,查看更多内容
随时随地看视频慕课网APP