Jump to content

sorting arrays using second dimension keys


Jim from Oakland

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.
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.