Jump to content


Photo

sorting many arrays by another array


  • Please log in to reply
2 replies to this topic

#1 fohanlon

fohanlon
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 15 March 2006 - 07:35 PM

Help,

I have 7 arrays all the same size:

$plans = array();
$supplier = array();
$plan_name = array();
$add_on = array();
$hosp_cover = array();
$excess = array();
$plancost = array();

I need to be able to sort the plancost array asc i.e. this contains a costing from lowest to highest. However I require the contents of each of the other arrays to be sorted to match the sorted positions of the plancost array.

I guess as the plancosts arrays sorts it sorts the values at the indexed entries in the other arrays to the sorted position. Hope you understand what I mean.

I tried looking into the sort function and the array_multisort function on php help online manuals but don't think they will slove my problem.

Thanks

Fergal.


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 15 March 2006 - 09:04 PM

array_multisort

$plans = array(2,3,1);
$supplier = array('sa','sb','sc');
$plan_name = array('pa','pb','pc');
$add_on = array('aa','ab','ac');
$hosp_cover = array('ha','hb','hc');
$excess = array('xa','xb','xc');
$plancost = array('ca','cb','cc');

array_multisort ($plans, $supplier, $plan_name, $add_on, $hosp_cover, $excess, $plancost);

echo '<pre>', print_r ($plans, true), '</pre>';
echo '<pre>', print_r ($supplier, true), '</pre>';
echo '<pre>', print_r ($plan_name, true), '</pre>';
echo '<pre>', print_r ($add_on, true), '</pre>';
echo '<pre>', print_r ($hosp_cover, true), '</pre>';
echo '<pre>', print_r ($excess, true), '</pre>';
echo '<pre>', print_r ($plancost, true), '</pre>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 15 March 2006 - 11:46 PM

Make sure the array you want to sort on is listed first, and be careful to choose numeric sort or string sort. String sort is the default.

$plancost = array(2,3,1);
$supplier = array('sa','sb','sc');
$plan_name = array('pa','pb','pc');
$add_on = array('aa','ab','ac');
$hosp_cover = array('ha','hb','hc');
$excess = array('xa','xb','xc');
$plans = array('ca','cb','cc');

array_multisort ($plancost, SORT_NUMERIC, SORT_ASC,
$supplier, $plan_name, $add_on, $hosp_cover, $excess, $plans);

echo '<pre>', print_r ($plans, true), '</pre>';
echo '<pre>', print_r ($supplier, true), '</pre>';
echo '<pre>', print_r ($plan_name, true), '</pre>';
echo '<pre>', print_r ($add_on, true), '</pre>';
echo '<pre>', print_r ($hosp_cover, true), '</pre>';
echo '<pre>', print_r ($excess, true), '</pre>';
echo '<pre>', print_r ($plancost, true), '</pre>';





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users