【问题标题】:Array multisort not working for an array数组多重排序不适用于数组
【发布时间】:2016-02-14 19:00:11
【问题描述】:
$artists = [
  0 => [
    "id" => "3",
    "plan_id" => "1",
    "name" => "Artist-A",
    "views" => "1189189",
    "soundcloud" => "42",
    "facebook" => "59881948",
    "twitter" => "21760757",
    "youtube" => 0,
    "instagram" => "3429017"
  ],
  1 => [
    "id" => "10",
    "plan_id" => "1",
    "name" => "Artist-B",
    "views" => "1",
    "soundcloud" => 0,
    "facebook" => 0,
    "twitter" => 0,
    "youtube" => 0,
    "instagram" => 0
  ],
  2 => [
    "id" => "2",
    "plan_id" => "1",
    "name" => "Artist-C",
    "views" => "1629",
    "soundcloud" => "20",
    "facebook" => "5025158",
    "twitter" => "582899",
    "youtube" => 0,
    "instagram" => "112127"
  ],
  3 => [
    "id" => "4",
    "plan_id" => "2",
    "name" => "Artist-D",
    "views" => "484353",
    "soundcloud" => "7",
    "facebook" => "104449606",
    "twitter" => "36820201",
    "youtube" => 0,
    "instagram" => "16483226"
  ],
  4 => [
    "id" => "5",
    "plan_id" => "2",
    "name" => "Artist-E",
    "views" => "98765432",
    "soundcloud" => "13",
    "facebook" => "59551072",
    "twitter" => "38995648",
    "youtube" => 0,
    "instagram" => "64997436"
  ]
]

foreach ($remaining_artists as $key => $value) {
   $soundcloud[$key] = $value['soundcloud'];
}

array_multisort($soundcloud, SORT_ASC, $artists);

我使用 array_multisort 对数组进行排序。它工作得很好。但是上面的数组有一个错误'array_multisort(): Array size are contrasts'。我实在想不通这里的问题和解决方法。

【问题讨论】:

    标签: php sorting array-multisort


    【解决方案1】:

    您的 array_multisort() 参数有问题。

    http://php.net/manual/en/function.array-multisort.php

    还有语法问题。

    这行得通

    <?php
    
    $artists = [
      0 => [
        "id" => "3",
        "plan_id" => "1",
        "name" => "Artist-A",
        "views" => "1189189",
        "soundcloud" => "42",
        "facebook" => "59881948",
        "twitter" => "21760757",
        "youtube" => 0,
        "instagram" => "3429017"
      ],
      1 => [
        "id" => "10",
        "plan_id" => "1",
        "name" => "Artist-B",
        "views" => "1",
        "soundcloud" => 0,
        "facebook" => 0,
        "twitter" => 0,
        "youtube" => 0,
        "instagram" => 0
      ],
      2 => [
        "id" => "2",
        "plan_id" => "1",
        "name" => "Artist-C",
        "views" => "1629",
        "soundcloud" => "20",
        "facebook" => "5025158",
        "twitter" => "582899",
        "youtube" => 0,
        "instagram" => "112127"
      ],
      3 => [
        "id" => "4",
        "plan_id" => "2",
        "name" => "Artist-D",
        "views" => "484353",
        "soundcloud" => "7",
        "facebook" => "104449606",
        "twitter" => "36820201",
        "youtube" => 0,
        "instagram" => "16483226"
      ],
      4 => [
        "id" => "5",
        "plan_id" => "2",
        "name" => "Artist-E",
        "views" => "98765432",
        "soundcloud" => "13",
        "facebook" => "59551072",
        "twitter" => "38995648",
        "youtube" => 0,
        "instagram" => "64997436"
      ]
    ];
    
    $soundcloud = [];
    
    foreach ($artists as $key => $value) {
       $soundcloud[$key] = $value['soundcloud'];
    }
    
    array_multisort($soundcloud, $artists,  SORT_ASC);
    
    print_r($artists);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多