简体   繁体   English

PHP排序多维数组,其值形成不同的数组

[英]PHP Sort Multidimensional array with the values form different arrays

I have data like the following, 我有以下数据,

Bale # | Factory # | Design | Color    | Price   |
----------------------------------------------------------------
1      | PDX-1     | D1     | RED      | 10      |
1      | PDX-10    | D2     | BLUE     | 200     |
1      | PDX-2     | D3     | PINK     | Some int|
1      | PDX-3     | D1     | WHITE    | Some int|
2      | PDX-4     | D3     | APPLE    | Some int|
2      | PXX-56    | D3     | PINE     | Some int|
2      | XXX-1     | D1     | SILVER   | Some int|
1      | XXX-4     | D5     | BROWN    | Some int|
1      | DFX-1     | D5     | COFFEE   | Some int|
3      | ABC-1     | D6     | PURPLE   | Some int|
1      | ABC-2     | D6     | GOLD     | Some int|

This is in multi dimensional array. 这是多维数组。 Where I put the Bale# in key, and other values in sub array against the bale. 我把Bale#放在key中,其他值放在子数组中对着bale。

forloop (...)
      (.....)
    $sorted_by_bale[$BALE_NO][] = array(
        'jnb' => $factory_number,
        'design_name' => $order_design,
        'colorway' => $order_colorway,
        'usd_rate' => $price,
    );
}

I require to sort the values order by bale, and then telling the total price for one bale and number of items in a bale. 我需要按打包顺序对值进行排序,然后告诉一个打包的总价格和打包中的项目数量。

ksort($sorted_by_bale);

Ksort served the purpose. Ksort服务于此目的。

Now I need to sort by Design (first) and then Color (second) within bale. 现在我需要按照设计(第一个)排序,然后在打包中按颜色(第二个)排序。

You should use usort (see documentation ). 你应该使用usort (参见文档 )。 This allows you to use your own sort function. 这允许您使用自己的排序功能。

One downside is that the array keys are reset. 一个缺点是数组键被重置。 But this does not seem to matter to your secondary arrays. 但这对您的辅助阵列似乎并不重要。

function sortBale($first, $second)
{
  if($first['design_name'] > $second['design_name']) {
    return 1;
  } elseif( $first['design_name'] < $second['design_name'] ) {
    return -1;
  } else {
    //they have the same design
    if($first['colorway'] > $second['colorway']) {
      return 1;
    } elseif( $first['colorway'] < $second['colorway'] ) {
      return -1;
    } else {
      return 0;
    }
  }
}

You need to us usort with a custom sort function: 您需要我们使用自定义排序功能:

 //once your bale array is setup

 usort($sorted_by_bale, "compare_in_bale");



 function compare_in_bale($a, $b)
  {
    if ($a['design_name'] == $b['design_name']) {
      if ($a['colorway'] == $b['colorway']) {
            //identical
           return 0;
      }
      return ($a['colorway']  < $b['colorway'])) ? -1 : 1;
  }
  return ($a['design_name']  < $b['design_name'])) ? -1 : 1;
 }

Note, you might want a better comparison operator than == but this should get you close enough. 注意,您可能需要一个比==更好的比较运算符,但这应该让您足够接近。

I could be missing your point, but try this: 我可能会错过你的观点,但试试这个:

<?php
$data = array(
    array(
        'bale' => 2,
        'design' => 'D1',
        'color' => 'RED'
    ),
    array(
        'bale' => 2,
        'design' => 'D1',
        'color' => 'BLUE'
    ),
    array(
        'bale' => 1,
        'design' => 'D2',
        'color' => 'BLUE'
    ),
    array(
        'bale' => 2,
        'design' => 'D3',
        'color' => 'PINK'
    ),
    array(
        'bale' => 1,
        'design' => 'D1',
        'color' => 'WHITE'
    ),
);

$bale = array();
$design = array();
$color = array();
// Obtain a list of columns
foreach ($data as $key => $row) {
    $bale[$key]  = $row['bale'];
    $design[$key] = $row['design'];
    $color[$key] = $row['color'];
}
array_multisort($bale, SORT_ASC, $design, SORT_ASC, $color, SORT_ASC, $data);
var_dump($data);
?>

The result is: 结果是:

array(5) {
  [0]=>
  array(3) {
    ["bale"]=>
    int(1)
    ["design"]=>
    string(2) "D1"
    ["color"]=>
    string(5) "WHITE"
  }
  [1]=>
  array(3) {
    ["bale"]=>
    int(1)
    ["design"]=>
    string(2) "D2"
    ["color"]=>
    string(4) "BLUE"
  }
  [2]=>
  array(3) {
    ["bale"]=>
    int(2)
    ["design"]=>
    string(2) "D1"
    ["color"]=>
    string(4) "BLUE"
  }
  [3]=>
  array(3) {
    ["bale"]=>
    int(2)
    ["design"]=>
    string(2) "D1"
    ["color"]=>
    string(3) "RED"
  }
  [4]=>
  array(3) {
    ["bale"]=>
    int(2)
    ["design"]=>
    string(2) "D3"
    ["color"]=>
    string(4) "PINK"
  }
}

Also check out http://php.net/manual/en/function.array-multisort.php 另请查看http://php.net/manual/en/function.array-multisort.php

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

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