Sort array

How can I sort the following array so that it is sorted by the first of the two values, in descending order? (i.e. 16, 10, 1)

Array
(
    [0] => stdClass Object
        (
            [name] => undefined
            [values] => Array
                (
                    [0] => 16
                    [1] => 0
                )

        )

    [1] => stdClass Object
        (
            [name] => GBP
            [values] => Array
                (
                    [0] => 1
                    [1] => 0.01
                )

        )

    [2] => stdClass Object
        (
            [name] => EUR
            [values] => Array
                (
                    [0] => 10
                    [1] => 2.37
                )

        )

)

Open in new window

LVL 6
benwiggyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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:
See if this does the trick.  I always have to look up uSort() -- for some reason I can't remember exactly how the moving parts work, so I keep a few samples in my teaching library.
http://www.laprbass.com/RAY_temp_benwiggy.php

<?php // RAY_temp_benwiggy.php

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28233759.html

/* PROBLEM DEFINITION
{"value": {"board": [
{"name": "undefined", "values": [16, 0]},
{"name": "GBP", "values": [1, 0.1]},
{"name": "EUR", "values": [10, 2.37]}
]}}
*/

error_reporting(E_ALL);

// A GATHERING FUNCTION
function gather($obj, $sig)
{
    $sum = 0;
    foreach ($obj->data->values->$sig as $val)
    {
        $sum += $val;
    }
    return $sum;
}

// A SORTING FUNCTION
function mysort($a, $b)
{
    if     ($a->values[0] == $b->values[0]) return 0;
    return ($a->values[0]  > $b->values[0]) ? -1 : 1;
}


// READ THE JSON STRINGS
$url = 'http://filedb.experts-exchange.com/incoming/2013/09_w36/676024/raw-json1';
$str = file_get_contents($url);
$fee = json_decode($str);

$url = 'http://filedb.experts-exchange.com/incoming/2013/09_w36/676025/raw-json2';
$str = file_get_contents($url);
$trn = json_decode($str);

// ACQUIRE THE NAMES OF FIELDS WE WANT TO FIND
foreach ($fee->data->values as $key => $nothing)
{
    $fields[$key] = $key;
}

// GATHER FEES
$a = array();
foreach ($fields as $f)
{
    $a[$f] = gather($fee, $f);
}

// GATHER TRANSACTIONS
$b = array();
foreach ($fields as $f)
{
    $b[$f] = gather($trn, $f);
}

// COMBINE THE ARRAYS INTO OBJECTS AND THE OBJECTS INTO AN ARRAY
$n = array();
foreach ($a as $key => $val)
{
    $o = new stdClass;
    $o->name = $key;
    $v = array($b[$key], $a[$key]);
    $o->values = $v;
    $n[] = $o;
}

// SORT
usort($n, 'mysort');

// CREATE THE BOARD OBJECT
$b = new stdClass;
$b->board = $n;

// CREATE THE COMPLETE REPRESENTATION
$c = new stdClass;
$c->value = $b;
echo json_encode($c);

Open in new window

Regards, ~Ray
0

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
benwiggyAuthor Commented:
perfect, thank you :)
0
Ray PaseurCommented:
Glad to help!  Thanks for the points and best of luck with your project, ~Ray
0
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.