1

Basically i need to sort some data into the top 5 best sales. I've managed to group the 3 arrays i've used using.

$c = array_combine($PriceArray,$namesArray,$ProductArray);
krsort($c); 

Price array being the total. (Bit of a silly name, i just realised) namesarray is the array of names and product array is the list of product codes

I need to print it in a table like

"|£3.45|Jelly Beans | 120|" so they have their own column, but at the moment it's printing it like | 3.45| array | array|

and i use

 echo '<td>'.$ProductArray[$i].'</td>'.'<td>'.$year.'</td>'.'<td>'.array_keys($c,$c[$i]).'<td>'. $PriceArray[$i].'</td>';

to print it.

Thanks in advance

    Array
    (

    [0] => 77358.47
    [1] => 111004.98
    [2] => 227194.9
    [3] => 84645.75
    [4] => 29693.58
    [5] => 198867.2
    [6] => 110779.5
    [7] => 210530.62
    [8] => 102916.79
    [9] => 186630.75
    [10] => 140143.24
    [11] => 48984.48
    [12] => 74759.34
    [13] => 204793.14
    [14] => 82192.5
    [15] => 167402.7
    [16] => 58232.72
    [17] => 216302.32
    [18] => 26353.92
    [19] => 149993.1

)

Array
(
    [0] => Jelly beans
    [1] => Banana milkshake powder
    [2] => Edam Cheese
    [3] => Hairnet
    [4] => Aubergine jam
    [5] => White bread
    [6] => Brown bread
    [7] => Purple bread
    [8] => Plain flour
    [9] => Striped flour
    [10] => Soft tissues
    [11] => Anti personnel mines
    [12] => Chicken fillets
    [13] => Beef cubes
    [14] => Marshmallows
    [15] => Fizzy carrot juice
    [16] => Low fat lard
    [17] => Suet dumpling mix
    [18] => Gravy powder
    [19] => Cherry pie filling
)
Array
(
    [0] => 121
    [1] => 122
    [2] => 123
    [3] => 124
    [4] => 125
    [5] => 126
    [6] => 127
    [7] => 128
    [8] => 129
    [9] => 130
    [10] => 131
    [11] => 132
    [12] => 133
    [13] => 134
    [14] => 135
    [15] => 136
    [16] => 137
    [17] => 138
    [18] => 139
    [19] => 140

) Product Code Year Name Sales Total

Rhys Drury
  • 305
  • 2
  • 20

2 Answers2

0

Zip function blatantly stolen from: zip function from SO

function zip() {
  $args = func_get_args();
  $zipped = array();
  $n = count($args);
  for ($i=0; $i<$n; ++$i) {
    reset($args[$i]);
  }
  while ($n) {
    $tmp = array();
    for ($i=0; $i<$n; ++$i) {
      if (key($args[$i]) === null) {
        break 2;
      }
      $tmp[] = current($args[$i]);
      next($args[$i]);
    }
    $zipped[] = $tmp;
  }
  return $zipped;
}

function cmp($a, $b)
{
  if($a[0] == $b[0]){
    return 0;
  }
  return ($a[0] < $b[0]) ? -1 : 1;
}

$PriceArray   = array( 4.56, 1.23, 7.89 );
$namesArray   = array( 'ab', 'cd', 'ef' );
$ProductArray = array( '11', '22', '33' );

$c = zip($PriceArray, $namesArray, $ProductArray);

usort($c, 'cmp');

foreach($c as $prices)
{
  //$prices[0] == price
  //$prices[1] == name
  //$prices[2] == code
  echo "{$prices[0]}|{$prices[1]}|{$prices[2]}\n";

prints:

1.23|cd|22
4.56|ab|11
7.89|ef|33

I am using the zip-function that is available natively in Python to combine N-arrays and "zip" them together.
So take index 0 of all given arrays, and make that a new array entry. Do that for all indices available.

The cmp function takes two variables, in this case two arrays, where index-0 = price, 1 = name and 2 = code. You obviously want to sort by ascending by price thus we are comparing the price index. This results in a new/sorted array based on price.

You can also substitude the usort($c, 'cmp'); call with the following:

usort($c, function($a, $b){
  if($a[0] == $b[0]){
    return 0;
  }
  return ($a[0] < $b[0]) ? -1 : 1;
});

However, this is only available in PHP version >= 5.3

Community
  • 1
  • 1
Daan Timmer
  • 14,771
  • 6
  • 34
  • 66
  • I just put the keys and values into new arrays, and that seemed to work, but thanks $NewNamesArray = array_values($c); $NewPriceArray = array_keys($c); – Rhys Drury May 04 '12 at 13:25
0

Just put the keys and values into new arrays.

$NewNamesArray = array_values($c); $NewPriceArray = array_keys($c)

Rhys Drury
  • 305
  • 2
  • 20