PHP - Sort Multi-Dimensional Array

Matthew Roessner
Matthew Roessner used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016
Commented:
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

Matthew RoessnerSenior Systems Programmer

Author

Commented:
Example of Data:

I attached a word document with some sample dataPHP_Array_Example_Data.doc
Most Valuable Expert 2011
Top Expert 2016

Commented:
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!
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Matthew RoessnerSenior Systems Programmer

Author

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
Most Valuable Expert 2011
Top Expert 2016

Commented:
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 Programmer

Author

Commented:
I was able to come up with a solution based on your example. Thanks for posting...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial