PHP:获取多维数组的前5个值

时间:2012-02-13 06:43:24

标签: php

我有一个数组:

Array ( [0] => Array ( [trade] => Sealwell
[open] => 7
[closed] => 0
) [1] => Array ( [trade] => Add-a-Line
[open] => 2
[closed] => 0
) [2] => Array ( [trade] => C.T.A
[open] => 5
[closed] => 0
) [3] => Array ( [trade] => Chadoak
[open] => 1
[closed] => 0
) [4] => Array ( [trade] => Chevron
[open] => 0
[closed] => 1
) [5] => Array ( [trade] => City Pro Sign
[open] => 8
[closed] => 10
) [6] => Array ( [trade] => ControlFab
[open] => 4
[closed] => 0
) [7] => Array ( [trade] => Detail
[open] => 1
[closed] => 0
) [8] => Array ( [trade] => Fabcot to advise
[open] => 1
[closed] => 0
) [9] => Array ( [trade] => FireBase
[open] => 2
[closed] => 0
) [10] => Array ( [trade] => Gemwood
[open] => 1
[closed] => 0
) [11] => Array ( [trade] => HG
[open] => 3
[closed] => 0
) [12] => Array ( [trade] => I.E.S
[open] => 11
[closed] => 30
) [13] => Array ( [trade] => Integral
[open] => 5
[closed] => 0
) [14] => Array ( [trade] => McGee
[open] => 26
[closed] => 0
) [15] => Array ( [trade] => McGee / Detail
[open] => 1
[closed] => 0
) [16] => Array ( [trade] => McGee / Sealwell
[open] => 1
[closed] => 0
) [17] => Array ( [trade] => McGee / Spence
[open] => 0
[closed] => 1
) [18] => Array ( [trade] => Meridian
[open] => 7
[closed] => 0
) [19] => Array ( [trade] => Mulgrave
[open] => 2
[closed] => 0
) [20] => Array ( [trade] => Mulgrave / McGee
[open] => 1
[closed] => 0
) [21] => Array ( [trade] => Precast
[open] => 1
[closed] => 0
) [22] => Array ( [trade] => R&D
[open] => 1
[closed] => 0
) [23] => Array ( [trade] => Spence
[open] => 1
[closed] => 0
) [24] => Array ( [trade] => Star
[open] => 2
[closed] => 0
) [25] => Array ( [trade] => Tieman
[open] => 2
[closed] => 0
) [26] => Array ( [trade] => TTI
[open] => 3
[closed] => 0
) ) 

我希望以最高开放价值获得前5名交易,以及最高开放数+最高开数前5名,比如

具有最高开放价值的前5大交易

Array ( [0] => Array ( [trade] => McGee) [open] => 26 ) 
( [1] => Array ( [trade] => I.E.S) [open] => 11 ) 
( [2] => Array ( [trade] => City Pro Sign) [open] => 8 ) 
( [3] => Array ( [trade] => Meridian) [open] => 7 ) 
( [4] => Array ( [trade] => Sealwell) [open] => 7 ) 
)

开放+已关闭次数最多的前5名交易

Array ( [0] => Array ( [trade] => I.E.S) [open] => 11 [closed] => 30 [count] => 41) 
    ( [1] => Array ( [trade] => McGee) [open] => 26 [closed] => 0 [count] => 26) 
    ( [2] => Array ( [trade] => City Pro Sign) [open] => 8 [closed] => 10 [count] => 18) 
    ( [3] => Array ( [trade] => Meridian) [open] => 7 [closed] => 0 [count] => 7) 
    ( [4] => Array ( [trade] => Sealwell) [open] => 7 [closed] => 0 [count] => 7) 
    )

2 个答案:

答案 0 :(得分:0)

  1. 正确格式化数组。你的例子在php中不起作用。

    $ arrays = array(array(“trade”=>“Sealwell”,                         “open”=> 7,                         “封闭”=> 0,                       ),
                     array(“trade”=>“Add-a-Line”,                         “open”=> 2,                         “封闭”=> 0                       ));

  2. 尝试使用。例如,您可以编写两个不同的函数。一个比较“开放”和一个比较“开放”+“关闭”的一个。 这是一个快速实施,你将需要锻炼其余的。

    function compareOpenClose($a, $b) {
      $x = $a['open'] + $a['closed'];
      $y = $b['open'] + $b['closed'];
      if ($x == $y) {
        return 0;
      } else if ( $x > $y ) {
        return -1;
      } else {
        return 1;
      }
    }
    
    
    usort($arrays, 'compareOpenClose');
    print_r($arrays);
    

答案 1 :(得分:0)

具有最高开放价值的前5大交易

首先重新格式化数组

Array ( [Sealwell] => 7 [Add-a-Line] => 2 [C.T.A] => 5 [Chadoak] => 1 [Chevron] => 0 [City Pro Sign] => 8 [ControlFab] => 4 [Detail] => 1 [Fabcot to advise] => 1 [FireBase] => 2 [Gemwood] => 1 [HG] => 3 [I.E.S] => 11 [Integral] => 5 [McGee] => 26 [McGee / Detail] => 1 [McGee / Sealwell] => 1 [McGee / Spence] => 0 [Meridian] => 7 [Mulgrave] => 2 [Mulgrave / McGee] => 1 [Precast] => 1 [R&D] => 1 [Spence] => 1 [Star] => 2 [Tieman] => 2 [TTI] => 3 ) 

然后在单维数组中使用arsort($top_5_td,SORT_NUMERIC);推送完整结果

$i=0;
foreach($top_5_td as $key => $val){
    $top_5_td_op[$i]="<td>Trade ".$key. "</td><td>Status " . $val . "</td>";
    $i++;
}

<table>
    <?php
    for($i=0;$i<5;$i++){
            echo '<tr>';
        echo $top_5_td_op[$i];
        echo '</tr>';
    }
    ?>
</table>