duanjiao1256 2011-10-20 22:44
浏览 73

如何按键值对数组的输出进行排序

I have a function that outputs details of a property and need to sort the results by price/sqft.

<?php
    //get search results!

    if ($dbh)
    { 

        $sql = sql_spShowMemberSearch($_SESSION['r_search']); //create query from search vals
        $tout .= "<p><b>sql_spShowMemberSearch</b>: " . $sql . "</p>";


        $result=odbc_exec($dbh, $sql); //create db recordset

        if (isset($r_results)) { unset($r_results); }
        if (isset($_SESSION['r_results'])) { unset($_SESSION['r_results']); }
        //store recordset in array for use after closing db connection

        while(odbc_fetch_row($result)) // getting data
        {
            $rowcount++;
            //store values needed       
            //$r_results[$rowcount]['IsUs']             = odbc_result($result, 1); // IsUs
            $r_results[$rowcount]['intID']          = odbc_result($result, 2); // intID
            $r_results[$rowcount]['currentprice']   = odbc_result($result, 3); // CurrentPrice
            $r_results[$rowcount]['beds']           = odbc_result($result, 4); // Beds
            $r_results[$rowcount]['bath']           = odbc_result($result, 5); // Bath
            $r_results[$rowcount]['year']           = odbc_result($result, 6); // Year
            $r_results[$rowcount]['sf']             = odbc_result($result, 7); // SF
            $r_results[$rowcount]['address']        = odbc_result($result, 8); // Address
            $r_results[$rowcount]['community']      = odbc_result($result, 9); // Community
            $r_results[$rowcount]['office']         = odbc_result($result, 10); // Office
            $r_results[$rowcount]['agent']          = odbc_result($result, 11); // Agent
            //$r_results[$rowcount]['Type']             = odbc_result($result, 12); // Type
            $r_results[$rowcount]['photopath']      = odbc_result($result, 13); // PhotoPath
            $r_results[$rowcount]['pics']           = odbc_result($result, 14); // Pics
            $r_results[$rowcount]['mlsid']          = odbc_result($result, 15); // MLSID
            $r_results[$rowcount]['city']           = odbc_result($result, 16); // City
            $r_results[$rowcount]['long']           = odbc_result($result, 17); // Long
            $r_results[$rowcount]['lat']            = odbc_result($result, 18); // Lat
            $r_results[$rowcount]['source']         = odbc_result($result, 19); // Source
            //$r_results[$rowcount]['Tracked']      = odbc_result($result, 20); // Tracked
            $r_results[$rowcount]['status']         = odbc_result($result, 21); // Status
            //$r_results[$rowcount]['PageRow']      = odbc_result($result, 22); // PageRow

            $r_results[$rowcount]['ppsq']       = number_format(odbc_result($result, 3)/odbc_result($result, 7)); // Status


            $plcity = strtoupper(trim(preg_replace("/[^A-Za-z0-9[:space:]]/","",$r_results[$rowcount]['city'])));
            $plcity = str_replace(" ","-",$plcity);
            $pladdr = strtoupper(trim(preg_replace("/[^A-Za-z0-9[:space:]]/","",$r_results[$rowcount]['address'])));
            $pladdr = str_replace(" ","-",$pladdr);
            $plmls = trim($r_results[$rowcount]['mlsid']);
            $r_results[$rowcount]['permalink'] = "http://www.fairplay.com/realty/details/".$plcity."/".$pladdr."/".$plmls;

        }


        if ($rowcount==0) {
            if ($page_current == 1 ) {
                $e_msg[] = 'No results found! Please modify your search parameters (above).';
            } else {
                $e_msg[] = 'We ran out of search results, please go back or modify your search criteria to find additional matches.';
            }

        } 

        if ( $rowcount < $page_size ) {
            //since we can't rely on DB result for total count try not to let users page to non existant pages
            $_SESSION['r_search']['total_results'] = ($_SESSION['r_search']['page_start'] + ($rowcount-1)); 

         }

        //close connection
        odbc_close ($dbh);
    } //if $dbh
    else echo "odbc not connected <br>";
    //end get search results

function display_results($r_results) {
    $rowcount = 0;


    foreach ($r_results as $key=>$value) {

        $rowcount ++;       
        if ( $rowcount == 1 ) {
            echo "
<h3>Results for Real Estate in <span class='location'>".strtoupper($_SESSION['r_search']['searchtext'])."</span></h3>";
            echo "
<h4><strong>".number_format($_SESSION['r_search']['total_results'])."</strong> properties match your search</h4>
";
            echo "
<table id=\"realty\">
";
            echo "<thead>
";                                          
            echo "<th class=\"photos\">Photos</th> 
";                                        
            echo "<th class=\"address\">Address</th> 
"; 
            echo "<th class=\"community\">Community</th> 
";                                                  
            echo "<th class=\"numeric\">Price</th> 
";                                                            
            echo "<th class=\"beds\">Beds</th> 
";                                    
            echo "<th class=\"bath\">Bath</th> 
";                                    
            echo "<th class=\"year\">Year</th> 
";
            echo "<th class=\"numeric sf\">SQFT</th> 
";                                                                                  
            echo "<th class=\"mls\">MLS #</th> 
";
            echo "<th class=\"status\">Status</th> 
";
            echo "<th>&nbsp;</th> 
";                 
            echo "</thead> 
";
        }

        //  echo "Row: $key<br/>";
        //  make row
        echo "<tr> 
";
        echo "<td class=\"photos\"><a style=\"cursor:pointer;\" target=\"_blank\" href=\"/realty/detail/".$r_results[$rowcount]['intID']."\"><img src=\"";

        if ( isset($r_results[$key]['photopath']) ) {
            echo "http://www.foreclosuredatasystems.com/images/FullMLS/" . str_replace("\\","/",$r_results[$key]['photopath'])."/".$r_results[$key]['mlsid'] . "-01.jpg";
        } else {
            echo "http://fairplay.com/img/fr_home_thumb.jpg";
        }

        //echo str_replace("\\","/",$r_results[$key]['photopath'])."/".$r_results[$key]['mlsid'] ;
        echo "\" alt=\"photographs provided by MLS listing service providers\" width=\"100\" /></a></td> 
";    
        echo "<td class=\"address\"> 
";
        echo "<p>".$r_results[$key]['address']."<br /> 
";
        echo " ".$r_results[$key]['city']."</p> 
";
        echo "<!--<a href=\"#?lat=".$r_results[$key]['lat']."&long=".$r_results[$key]['long']."\" class=\"map\">Map this</a>--> 
";
        echo "</td> 
";
        echo "<td class=\"community\">".$r_results[$key]['community']."</td>                             
";
        echo "<td class=\"price\">\$".number_format($r_results[$key]['currentprice'])."</td>                         
";
        echo "<td class=\"beds\">".$r_results[$key]['beds']."</td> 
";
        echo "<td class=\"bath\">".$r_results[$key]['bath']."</td> 
";
        echo "<td class=\"year\">".$r_results[$key]['year']."</td> 
";
        echo "<td class=\"sf\">".number_format($r_results[$key]['sf'])."</td> 
";
        echo "<td class=\"mls\">".$r_results[$key]['mlsid']."</td> 
";
        echo "<td class=\"status\">".$r_results[$key]['status']."</td> 
";
        echo "<td class=\"action\"><a href=\"/realty/detail/".$r_results[$rowcount]['intID']."\">View Details</a><!--".$key."--></td> 
";       
        echo "</tr> 
";
        echo "<tr class=\"extra\"> 
";
        echo "<td class=\"photos\"><!--<a href=\"#\">".$r_results[$key]['pics']." photos</a>--></td> 
";
        echo "<td colspan=\"10\" class=\"source\">Listing courtesy of: ".$r_results[$key]['source']." / ".$r_results[$key]['office']."</td> 
";
        echo "</tr> 
";    

    } // END FOREACH LOOP

    echo "</table >
";
} //function display_results()

?>

How can I sort the output of these results by $r_results[$rowcount]['ppsq']?

  • 写回答

1条回答 默认 最新

  • dp5657087 2011-10-20 22:46
    关注

    You are in luck, this is a php built in function:

    http://www.php.net/manual/en/function.array-multisort.php

    Example:

    <?php
    
    $r_results = array();
    $r_results[0] = array('photopath' => '/photo/1', 'ppsq' => 12);
    $r_results[1] = array('photopath' => '/photo/7', 'ppsq' => 456);
    $r_results[2] = array('photopath' => '/photo/2', 'ppsq' => 789);
    $r_results[3] = array('photopath' => '/photo/9', 'ppsq' => 123);
    $r_results[4] = array('photopath' => '/photo/3', 'ppsq' => 23);
    $r_results[5] = array('photopath' => '/photo/5', 'ppsq' => 1);
    
    $ppsqs = array();
    
    foreach ($r_results as $key => $arr) {
      $ppsqs[$key] = $arr['ppsq'];
    }
    
    array_multisort($ppsqs, SORT_DESC, $r_results);
    
    echo $r_results[0]['photopath'];
    echo "
    ";
    echo $r_results[1]['photopath'];
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog