Jump to content

Archived

This topic is now archived and is closed to further replies.

Jim from Oakland

sorting arrays using second dimension keys

Recommended Posts

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.

Share this post


Link to post
Share on other sites
this takes your array, shuffles it, then put it back into order again

[code]<?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>';
?>[/code]

Share this post


Link to post
Share on other sites
be careful to set up your array in this order. 1st set category then subject and at last response

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.