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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.