Jump to content


Photo

sorting arrays using second dimension keys


  • Please log in to reply
3 replies to this topic

#1 Jim from Oakland

Jim from Oakland
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 04 September 2006 - 07:09 PM

Phreax

User responses to individual forms are stored in arrays as shown below.  I will gather all such results into one large results array with the same structure/keys.  I'd sure appreciate your assistance with a way to sort the results array by each of the three second dimension keys, as described below. 

Responses from one form:

aItemsArray[1]['Category']=1;
aItemsArray[1]['Subject']="Car Color";
aItemsArray[1]['Response']="Blue";

aItemsArray[2]['Category']=1;
aItemsArray[2]['Subject']="Car Size";
aItemsArray[2]['Response']="Small";

aItemsArray[3]['Category']=2;
aItemsArray[3]['Subject']="House Color";
aItemsArray[3]['Response']="Orange";

aItemsArray[4]['Category']=2;
aItemsArray[4]['Subject']="House Size";
aItemsArray[4]['Response']="Medium";

For the results array: The order (of the first dimension numeric "index" keys) of the results array reflects a) items being grouped (sorted) by category, b) within each category items are sorted by Subject and c) within each subject items are sorted by Response.


#2 Barand

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

Posted 04 September 2006 - 07:19 PM

this takes your array, shuffles it, then put it back into order again

<?php
$aItemsArray[1]['Category']=1;
$aItemsArray[1]['Subject']="Car Color";
$aItemsArray[1]['Response']="Blue";

$aItemsArray[2]['Category']=1;
$aItemsArray[2]['Subject']="Car Size";
$aItemsArray[2]['Response']="Small";

$aItemsArray[3]['Category']=2;
$aItemsArray[3]['Subject']="House Color";
$aItemsArray[3]['Response']="Orange";

$aItemsArray[4]['Category']=2;
$aItemsArray[4]['Subject']="House Size";
$aItemsArray[4]['Response']="Medium";

shuffle ($aItemsArray);
echo '<pre>', print_r($aItemsArray, true), '</pre>';

array_multisort($aItemsArray);  
echo '<pre>', print_r($aItemsArray, 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 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 04 September 2006 - 07:35 PM

be careful to set up your array in this order. 1st set category then subject and at last response

#4 Barand

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

Posted 04 September 2006 - 07:39 PM

Good point
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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users