简体   繁体   English

按自定义顺序对数组的 php 数组进行排序

[英]Sorting a php array of arrays by custom order

I have an array of arrays:我有一个数组数组:

Array ( 
    [0] => Array (
        [id] = 7867867,
        [title] = 'Some Title'),
    [1] => Array (
        [id] = 3452342,
        [title] = 'Some Title'),
    [2] => Array (
        [id] = 1231233,
        [title] = 'Some Title'),
    [3] => Array (
        [id] = 5867867,
        [title] = 'Some Title')
)

The need to go in a specific order:需要按特定顺序进行:

  1. 3452342 3452342
  2. 5867867 5867867
  3. 7867867 7867867
  4. 1231233 1231233

How would I go about doing that?我该怎么做呢? I have sorted arrays before, and read plenty of other posts about it, but they are always comparison based (ie valueA < valueB).我以前对数组进行过排序,并阅读了大量关于它的其他帖子,但它们总是基于比较(即 valueA < valueB)。

Help is appreciated.感谢帮助。

I have an array of arrays:我有一个数组数组:

Array ( 
    [0] => Array (
        [id] = 7867867,
        [title] = 'Some Title'),
    [1] => Array (
        [id] = 3452342,
        [title] = 'Some Title'),
    [2] => Array (
        [id] = 1231233,
        [title] = 'Some Title'),
    [3] => Array (
        [id] = 5867867,
        [title] = 'Some Title')
)

The need to go in a specific order:需要按特定顺序进行:

  1. 3452342 3452342
  2. 5867867 5867867
  3. 7867867 7867867
  4. 1231233 1231233

How would I go about doing that?我将如何去做? I have sorted arrays before, and read plenty of other posts about it, but they are always comparison based (ie valueA < valueB).我之前对数组进行过排序,并阅读了许多其他文章,但它们始终基于比较(即valueA <valueB)。

Help is appreciated.感谢您的帮助。

I have an array of arrays:我有一个数组数组:

Array ( 
    [0] => Array (
        [id] = 7867867,
        [title] = 'Some Title'),
    [1] => Array (
        [id] = 3452342,
        [title] = 'Some Title'),
    [2] => Array (
        [id] = 1231233,
        [title] = 'Some Title'),
    [3] => Array (
        [id] = 5867867,
        [title] = 'Some Title')
)

The need to go in a specific order:需要按特定顺序进行:

  1. 3452342 3452342
  2. 5867867 5867867
  3. 7867867 7867867
  4. 1231233 1231233

How would I go about doing that?我将如何去做? I have sorted arrays before, and read plenty of other posts about it, but they are always comparison based (ie valueA < valueB).我之前对数组进行过排序,并阅读了许多其他文章,但它们始终基于比较(即valueA <valueB)。

Help is appreciated.感谢您的帮助。

I have an array of arrays:我有一个数组数组:

Array ( 
    [0] => Array (
        [id] = 7867867,
        [title] = 'Some Title'),
    [1] => Array (
        [id] = 3452342,
        [title] = 'Some Title'),
    [2] => Array (
        [id] = 1231233,
        [title] = 'Some Title'),
    [3] => Array (
        [id] = 5867867,
        [title] = 'Some Title')
)

The need to go in a specific order:需要按特定顺序进行:

  1. 3452342 3452342
  2. 5867867 5867867
  3. 7867867 7867867
  4. 1231233 1231233

How would I go about doing that?我将如何去做? I have sorted arrays before, and read plenty of other posts about it, but they are always comparison based (ie valueA < valueB).我之前对数组进行过排序,并阅读了许多其他文章,但它们始终基于比较(即valueA <valueB)。

Help is appreciated.感谢您的帮助。

I have an array of arrays:我有一个数组数组:

Array ( 
    [0] => Array (
        [id] = 7867867,
        [title] = 'Some Title'),
    [1] => Array (
        [id] = 3452342,
        [title] = 'Some Title'),
    [2] => Array (
        [id] = 1231233,
        [title] = 'Some Title'),
    [3] => Array (
        [id] = 5867867,
        [title] = 'Some Title')
)

The need to go in a specific order:需要按特定顺序进行:

  1. 3452342 3452342
  2. 5867867 5867867
  3. 7867867 7867867
  4. 1231233 1231233

How would I go about doing that?我将如何去做? I have sorted arrays before, and read plenty of other posts about it, but they are always comparison based (ie valueA < valueB).我之前对数组进行过排序,并阅读了许多其他文章,但它们始终基于比较(即valueA <valueB)。

Help is appreciated.感谢您的帮助。

I have an array of arrays:我有一个数组数组:

Array ( 
    [0] => Array (
        [id] = 7867867,
        [title] = 'Some Title'),
    [1] => Array (
        [id] = 3452342,
        [title] = 'Some Title'),
    [2] => Array (
        [id] = 1231233,
        [title] = 'Some Title'),
    [3] => Array (
        [id] = 5867867,
        [title] = 'Some Title')
)

The need to go in a specific order:需要按特定顺序进行:

  1. 3452342 3452342
  2. 5867867 5867867
  3. 7867867 7867867
  4. 1231233 1231233

How would I go about doing that?我将如何去做? I have sorted arrays before, and read plenty of other posts about it, but they are always comparison based (ie valueA < valueB).我之前对数组进行过排序,并阅读了许多其他文章,但它们始终基于比较(即valueA <valueB)。

Help is appreciated.感谢您的帮助。

I have an array of arrays:我有一个数组数组:

Array ( 
    [0] => Array (
        [id] = 7867867,
        [title] = 'Some Title'),
    [1] => Array (
        [id] = 3452342,
        [title] = 'Some Title'),
    [2] => Array (
        [id] = 1231233,
        [title] = 'Some Title'),
    [3] => Array (
        [id] = 5867867,
        [title] = 'Some Title')
)

The need to go in a specific order:需要按特定顺序进行:

  1. 3452342 3452342
  2. 5867867 5867867
  3. 7867867 7867867
  4. 1231233 1231233

How would I go about doing that?我将如何去做? I have sorted arrays before, and read plenty of other posts about it, but they are always comparison based (ie valueA < valueB).我之前对数组进行过排序,并阅读了许多其他文章,但它们始终基于比较(即valueA <valueB)。

Help is appreciated.感谢您的帮助。

I have an array of arrays:我有一个数组数组:

Array ( 
    [0] => Array (
        [id] = 7867867,
        [title] = 'Some Title'),
    [1] => Array (
        [id] = 3452342,
        [title] = 'Some Title'),
    [2] => Array (
        [id] = 1231233,
        [title] = 'Some Title'),
    [3] => Array (
        [id] = 5867867,
        [title] = 'Some Title')
)

The need to go in a specific order:需要按特定顺序进行:

  1. 3452342 3452342
  2. 5867867 5867867
  3. 7867867 7867867
  4. 1231233 1231233

How would I go about doing that?我将如何去做? I have sorted arrays before, and read plenty of other posts about it, but they are always comparison based (ie valueA < valueB).我之前对数组进行过排序,并阅读了许多其他文章,但它们始终基于比较(即valueA <valueB)。

Help is appreciated.感谢您的帮助。

I bump into this same problem and @mickmackusa has the answer I need.我遇到了同样的问题,@mickmackusa 有我需要的答案。 The selected answer does not sort when there is a NULL value.当存在NULL值时,所选答案不排序。 For example:例如:

$order = array(3, 2, 10);
$array = array(
    array('id' => NULL, 'title' => 'any order since null but not top'),
    array('id' => NULL, 'title' => 'any order since null but not top'),
    array('id' => NULL, 'title' => 'any order since null but not top'),
    array('id' => 2, 'title' => 'should be top'),
);
usort($array, function ($a, $b) use ($order) {
    $pos_a = array_search($a['id'], $order);
    $pos_b = array_search($b['id'], $order);
    return $pos_a - $pos_b;
});

The results above will show an output of:上面的结果将显示输出:

array(4) {
  [0]=>
  array(2) {
    ["id"]=>
    NULL
    ["title"]=>
    string(32) "any order since null but not top"
  }
  [1]=>
  array(2) {
    ["id"]=>
    NULL
    ["title"]=>
    string(32) "any order since null but not top"
  }
  [2]=>
  array(2) {
    ["id"]=>
    NULL
    ["title"]=>
    string(32) "any order since null but not top"
  }
  [3]=>
  array(2) {
    ["id"]=>
    int(2)
    ["title"]=>
    string(13) "should be top"
  }
}

In @mickmackusa's answer, not only it eliminates null in the sorting but also put in the first whatever is available in the order basis.在@mickmackusa 的回答中,它不仅消除了排序中的空值,而且还把顺序基础上可用的任何内容放在首位。 So since in the array the only available is 2 then that would be on top.因此,由于在数组中唯一可用的是 2,那么它将位于顶部。

Although it doesn't work in PHP 5.6.虽然它在 PHP 5.6 中不起作用。 So I convert it to PHP 5.6 compatible.所以我将它转换为 PHP 5.6 兼容。 This is what I got这就是我得到的

usort($array, function($a, $b) use($order, $default) {
    $a = (isset($order[$a['id']]) ? $order[$a['id']] : $default);
    $b = (isset($order[$b['id']]) ? $order[$b['id']] : $default);

    if($a == $b) return 0;
    elseif($a > $b) return 1;
    return -1;
});

The result for the sort above would be上述排序的结果将是

array(4) {
  [0]=>
  array(2) {
    ["id"]=>
    int(2)
    ["title"]=>
    string(13) "should be top"
  }
  [1]=>
  array(2) {
    ["id"]=>
    NULL
    ["title"]=>
    string(32) "any order since null but not top"
  }
  [2]=>
  array(2) {
    ["id"]=>
    NULL
    ["title"]=>
    string(32) "any order since null but not top"
  }
  [3]=>
  array(2) {
    ["id"]=>
    NULL
    ["title"]=>
    string(32) "any order since null but not top"
  }
}

I hope my conversion of the code would help dev who works on an obsolete server with lower php version.我希望我对代码的转换能帮助那些在 php 版本较低的过时服务器上工作的开发人员。

This is how I sort my multi-dimensional array in ASC order based on id value: 这是我根据id值以ASC顺序对多维数组进行排序的方式:

$arrayFilter = array(
    array('product_tag' => 'xenia', 'id' => 4),
    array('product_tag' => 'worn',  'id' => 5),
    array('product_tag' => 'woven', 'id' => 3),
    array('product_tag' => 'nude', 'id' => 1)
);

for ($i = 0; $i < sizeof($arrayFilter); $i++) {
    for ($j=$i+1; $j < sizeof($arrayFilter); $j++) {
        if ($arrayFilter[$i]['id'] > $arrayFilter[$j]['id']) {
            $c = $arrayFilter[$i];
            $arrayFilter[$i] = $arrayFilter[$j];
            $arrayFilter[$j] = $c;
        }
    }
}
print_r($arrayFilter);

OUTPUT: 输出:

Array
(
    [0] => Array
        (
            [product_tag] => nude
            [id] => 1
        )

    [1] => Array
        (
            [product_tag] => woven
            [id] => 3
        )

    [2] => Array
        (
            [product_tag] => xenia
            [id] => 4
        )

    [3] => Array
        (
            [product_tag] => worn
            [id] => 5
        )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM