Sorting an object based php array
<p>I am fairly new to programming and am trying to sort this array by username from a mssql query. have tried using a couple of sort functions that others have made on the web, but have had no luck Any help would be amazing!</p>

<pre><code>function getSalesPersonTargetTracker()
{
$targetTracker = new StdClass();
$targetTracker-&gt;agents = array();

$server = 'blah';
$connectioninfo = array( 'database' =&gt; 'blah', 'uid' =&gt; 'blah', 'pwd' =&gt; 'blah' );

$connection = sqlsrv_connect($server, $connectioninfo);

$query = "{call salespersontargettracker (?)}";
$params = array(
                 date_format(new DateTime(), '01/M/Y')
            );

//Run the Query             
$statement = sqlsrv_query($connection, $query, $params);

if (sqlsrv_fetch($statement) === false)
     print_r( sqlsrv_errors(), true);

//Read in the overall numbers
$targetTracker-&gt;target = (float)sqlsrv_get_field($statement, 0);
$targetTracker-&gt;workingDays = (int)sqlsrv_get_field($statement, 1);
$targetTracker-&gt;currentDay = (int)sqlsrv_get_field($statement, 2);
$targetTracker-&gt;dailyTarget = (float)sqlsrv_get_field($statement, 3);
$targetTracker-&gt;pointsShouldBeOn = (float)sqlsrv_get_field($statement, 4);
$targetTracker-&gt;pointsOn = (float)sqlsrv_get_field($statement, 5);
$targetTracker-&gt;pointsDifference = (float)sqlsrv_get_field($statement, 6);
$targetTracker-&gt;moneyShouldBeOn = (float)sqlsrv_get_field($statement, 7);
$targetTracker-&gt;moneyOn = (float)sqlsrv_get_field($statement, 8);
$targetTracker-&gt;moneyDifference = (float)sqlsrv_get_field($statement, 9);

//Move to the next result
sqlsrv_next_result($statement);

//Run through all of the agents and show their details
while (sqlsrv_fetch($statement))
{
    $salesPerson = new StdClass();
    $salesPerson-&gt;team = sqlsrv_get_field($statement, 0);
    $salesPerson-&gt;username = sqlsrv_get_field($statement, 1);
    $salesPerson-&gt;target = (float)sqlsrv_get_field($statement, 2);
    $salesPerson-&gt;workingDays = (int)sqlsrv_get_field($statement, 3);
    $salesPerson-&gt;currentDay = (int)sqlsrv_get_field($statement, 4);
    $salesPerson-&gt;dailyTarget = (float)sqlsrv_get_field($statement, 5);
    $salesPerson-&gt;pointsShouldBeOn = (float)sqlsrv_get_field($statement, 6);
    $salesPerson-&gt;pointsOn = (float)sqlsrv_get_field($statement, 7);
    $salesPerson-&gt;pointsDifference = (float)sqlsrv_get_field($statement, 8);
    $salesPerson-&gt;moneyShouldBeOn = (float)sqlsrv_get_field($statement, 9);
    $salesPerson-&gt;moneyOn = (float)sqlsrv_get_field($statement, 10);
    $salesPerson-&gt;moneyDifference = (float)sqlsrv_get_field($statement, 11);

    //Add this person to the people list
    $targetTracker-&gt;agents[] = $salesPerson;
}

return $targetTracker;
}
$targetTrackerall = getSalesPersonTargetTracker();

?&gt;
    &lt;table width="100%"&gt;
        &lt;thead&gt;
            &lt;tr&gt;
                &lt;th&gt;&lt;center&gt;Username&lt;/center&gt;&lt;/th&gt;
                &lt;th&gt;&lt;center&gt;Day&lt;/center&gt;&lt;/th&gt;   
                &lt;th&gt;&lt;center&gt;%&lt;/center&gt;&lt;/th&gt;     
                &lt;th&gt;&lt;center&gt;% +/-&lt;/center&gt;&lt;/th&gt;                 
                &lt;th&gt;&lt;center&gt;Actual Total&lt;/center&gt;&lt;/th&gt;
                &lt;th&gt;&lt;center&gt;+/- Total&lt;/center&gt;&lt;/th&gt;
            &lt;/tr&gt;
        &lt;/thead&gt;

    &lt;?php 
    //This loops through every single agent in our array
    foreach ($targetTrackerall-&gt;agents as $agent) {
        //If their points difference is a negative, then name &amp; shame them
        if ($agent-&gt;pointsDifference &lt; 0)
        {
            ?&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;b&gt;&lt;?php echo $agent-&gt;username ?&gt;&lt;/b&gt;&lt;/td&gt;
                &lt;td style="text-align: center;"&gt;&lt;?php echo $agent-&gt;currentDay  ?&gt; / &lt;b&gt;&lt;?php echo $agent-&gt;workingDays?&gt;&lt;/b&gt;&lt;/td&gt;
                &lt;td style="text-align: center;"&gt;&lt;?php echo $agent-&gt;pointsOn ?&gt;%&lt;/td&gt;
                &lt;td style="text-align: center;"&gt;&lt;?php echo round($agent-&gt;pointsDifference) ?&gt;%&lt;/td&gt;
                &lt;td style="text-align: center;"&gt;£&lt;?php echo number_format($agent-&gt;moneyOn) ?&gt;&lt;/td&gt;
                &lt;td style="color: red; text-align: center;"&gt;-£&lt;?php echo number_format(($agent-&gt;moneyDifference)*-1) ?&gt;&lt;/td&gt;

    &lt;?php

        }
    }
    ?&gt;
    &lt;/tr&gt;
    &lt;/table&gt;
</code></pre>
