1

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.

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.

Now I need to sort by Design (first) and then Color (second) within bale.

1
  • Using usort() sound like the way to go. Commented Nov 28, 2012 at 14:40

3 Answers 3

1

You should use usort (see documentation). 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;
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Note that $sorted_by_bale[$BALE_NO] is also multidimensional array
@AamirMahmood the function above assumes $a and $b are arrays. Call the usort outside your loop... my bad.
1

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

1 Comment

Still not completely following what your goal is. In my code, could you swap out $data with $sorted_by_bale[$BALE_NO]? Like this for example: pastebin.com/JcZEg9pL the output would be: pastebin.com/wNAaiGgK

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.