PHP - Sort Multi-Dimensional Array

I have the following array, that I create within a "while loop":

$drugs[$totalcount]['drug']=$drug;
$drugs[$totalcount]['ndc']=$ndc;
$drugs[$totalcount]['prevcost']=$prevcost;
$drugs[$totalcount]['newcost']=$newcost;
$drugs[$totalcount]['drugcount']=$drugcount;
$drugs[$totalcount]['pc']=$pc;
$drugs[$totalcount]['dcv']=$dcv;

$totalcount=$totalcount+1;

I want to sort the $drugs array based on the "dcv" portion of the array.  I plan to output the array using a "for loop" - and want to have the output which I am using for a report - to list everything starting with the smallest DCV to the largest DCV.

I was looking at trying to use USORT() - but wasn't sure if that was the right direction or not.

Any help would be very much appreciated.

Thanks
LVL 1
Matthew RoessnerSenior Systems ProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
USort() is probably right, but it  is hard to tell from the posted code sample.  Can you please show us some test data?  If we can get that we can probably show you a tested and working code example.

Here's a teaching example I've used in PHP classes.
<?php // RAY_usort_example.php
/**
 * Challenge: Sort these arrays in order by name or by 'group' where the group
 * is a concatenation of the elements of the 'list' sub-array
 */
error_reporting(E_ALL);
echo "<pre>";


// TEST DATA
$data[] = array('name' => 'h', 'list' => array(1,2));
$data[] = array('name' => 'g', 'list' => array(1));
$data[] = array('name' => 'a', 'list' => array(1,3));
$data[] = array('name' => 'f', 'list' => array(2));
$data[] = array('name' => 'e', 'list' => array(2,3));
$data[] = array('name' => 'b', 'list' => array(3));
$data[] = array('name' => 'c', 'list' => array(1,2,3,4));
$data[] = array('name' => 'd', 'list' => array(3,4));


// A FUNCTION TO COMPARE BY NAMES
function cmp_name($a, $b)
{
    if ($a["name"] == $b["name"]) return 0;
    return ($a["name"] < $b["name"]) ? -1 : 1;
}

// A FUNCTION TO COMPARE BY GROUPS
function cmp_group($a, $b)
{
    if ($a["group"] == $b["group"]) return 0;
    return ($a["group"] < $b["group"]) ? -1 : 1;
}


// COPY ARRAY AND SORT ON NAME
$test = $data;
usort($test, 'cmp_name');

// SHOW THE WORK PRODUCT
var_dump($test);


// COPY THE ARRAY AND ADD GROUPS
$test = $data;
$maxlen = 0;
foreach($test as $ptr => $arr)
{
    $test[$ptr]['group'] = implode('', $arr['list']);
    if (strlen($test[$ptr]['group']) > $maxlen) $maxlen = strlen($test[$ptr]['group']);
}

// PAD THE GROUP STRINGS TO CONSISTENT LENGTH TO ALLOW SORTING
foreach ($test as $ptr => $arr)
{
    $test[$ptr]['group'] = str_pad($test[$ptr]['group'], $maxlen+1, '0');
}

// SORT ON GROUPS
usort($test, 'cmp_group');

// OPTIONAL: REMOVE THE GROUPS (IF NEEDED)
foreach ($test as $ptr => $arr)
{
    unset($test[$ptr]['group']);
}

// SHOW THE WORK PRODUCT
var_dump($test);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Matthew RoessnerSenior Systems ProgrammerAuthor Commented:
Example of Data:

I attached a word document with some sample dataPHP_Array_Example_Data.doc
Ray PaseurCommented:
WordYou can use PHP var_export() to get a PHP variable representation of the data.  Makes it easy to plug into a PHP script and get some results!
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Matthew RoessnerSenior Systems ProgrammerAuthor Commented:
Sorry - I am not in front of my code right now. I also attached the output as a picture. Are you able to see this?
PHP_Example_Data.jpg
Ray PaseurCommented:
Yes, I can see the picture, but to turn the picture into test data I would have to retype all of the data, a line at a time, to create the test case.  Please give us the SSCCE, and I'll be glad to show you the specific code solution.  Thanks.
Matthew RoessnerSenior Systems ProgrammerAuthor Commented:
I was able to come up with a solution based on your example. Thanks for posting...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.