如何根据php中的另一个数组对数组进行排序?

时间:2015-11-07 08:50:34

标签: php arrays sorting

如何在php中基于另一个数组对数组进行排序?我有这个人名单,实际上他们是1300多人......我只会在这里展示22人:)

人员阵列:

Array ( 
    [0] => Array ( [name] => Kay [site] => ex [rate] => 10 ) 
    [1] => Array ( [name] => Kat [site] => ih [rate] => 9.7 )
    [2] => Array ( [name] => Kate [site] => tp [rate] => 9 )
    [3] => Array ( [name] => rina [site] => tc [rate] => 9.8 )
    [4] => Array ( [name] => Katay [site] => cfnm [rate] => 6.8 )
    [5] => Array ( [name] => teriay [site] => sn [rate] => 7.6 )
    [6] => Array ( [name] => Kaay [site] => tla [rate] => 9.7 )
    [7] => Array ( [name] => na Kay [site] => bsc [rate] => 9.5 )
    [8] => Array ( [name] => qwerty [site] => tdp [rate] => 9.5 )
    [9] => Array ( [name] => Katey [site] => hd [rate] => 9.4 )
    [10] => Array ( [name] => Kat Kay [site] => ss [rate] => 9.2 )
    [11] => Array ( [name] => ina Kay [site] => pv [rate] => 9.43 )
    [12] => Array ( [name] => ina [site] => rat [rate] => 9.32 )
    [13] => Array ( [name] => atay [site] => trw [rate] => 9.32 )
    [14] => Array ( [name] => erina [site] => tlm [rate] => 9.43 )
    [15] => Array ( [name] => Ky [site] => ol [rate] => 8.34 )
    [16] => Array ( [name] => ikay [site] => tb [rate] => 7.34 )
    [17] => Array ( [name] => jay [site] => ta [rate] => 6.5 )
    [18] => Array ( [name] => saday [site] => hfy [rate] => 4.6 )
    [19] => Array ( [name] => tarans [site] => sd [rate] => 6.54 )
    [20] => Array ( [name] => dastw [site] => si [rate] => 6.4 )
    [21] => Array ( [name] => dyr say [site] => ex [rate] => 7.6 )
) 

并且有一个名为site的另一个数组,我想相应地显示它们,每个循环每个站点只有一个人,当站点阵列到达终点时,它将开始上升直到人员列表完成阅读。

网站订单数组

Array ( [0] => Array ( [acronym] => exs [site_order] => 1 ) 
[1] => Array ( [acronym] => ts [site_order] => 1 ) 
[2] => Array ( [acronym] => ih [site_order] => 2 ) 
[3] => Array ( [acronym] => tp [site_order] => 3 ) 
[4] => Array ( [acronym] => tc [site_order] => 4 ) 
[5] => Array ( [acronym] => cfnm [site_order] => 5 ) 
[6] => Array ( [acronym] => sn [site_order] => 6 ) 
[7] => Array ( [acronym] => tla [site_order] => 7 ) 
[8] => Array ( [acronym] => bsc [site_order] => 8 ) 
[9] => Array ( [acronym] => tdp [site_order] => 9 ) 
[10] => Array ( [acronym] => lhd [site_order] => 10 ) 
[11] => Array ( [acronym] => ss [site_order] => 11 ) 
[12] => Array ( [acronym] => pov [site_order] => 12 ) 
[13] => Array ( [acronym] => rat [site_order] => 13 ) 
[14] => Array ( [acronym] => trw [site_order] => 14 ) 
[15] => Array ( [acronym] => tgs [site_order] => 15 ) 
[16] => Array ( [acronym] => tlm [site_order] => 16 ) 
[17] => Array ( [acronym] => ol [site_order] => 17 ) 
[18] => Array ( [acronym] => tb [site_order] => 18 ) 
[19] => Array ( [acronym] => ta [site_order] => 19 ) 
[20] => Array ( [acronym] => hfy [site_order] => 20 ) 
[21] => Array ( [acronym] => sd [site_order] => 21 ) 
[22] => Array ( [acronym] => si [site_order] => 22 ) 
[23] => Array ( [acronym] => tse [site_order] => 23 ) 
[24] => Array ( [acronym] => ih [site_order] => 24 ) 
)

我也希望按费率显示它们。但我甚至无法显示正确的数据,因为当网站数组foreach完成后,它会停止加载人。这是我的代码到目前为止......

$x = 0;
$num = 0;
foreach ($site as $item) {
        foreach ($data as $person)
        {
            if($site[$num]['acronym'] == $persons['site'] && $x != 1)
            {
                echo 'name: '.$person['site'].'<br>';
                echo 'site: '.$person['name'].'<br>';
                echo '<br>';

                $x++;
            }
        }
        $x = 0;
    $num++;
}

我想要的输出是例如:

site: ex
name: kay
rate: 10

site: tp
name: kate
rate: 9

site: tc
name: rina
rate: 9.8

site: ih
name: kat
rate: 9.7

site: tla
name: Kaay
rate: 9.7
.
.
.
and so on

请注意,我想要一个条件: 循环人按网站订单和按订单排序,但每个网站只有一个人循环。

我知道这听起来有点复杂,但请帮助我。我在这里搜索了很多与我有同样问题的帖子,但没有运气。 :'(

3 个答案:

答案 0 :(得分:2)

以下代码是一种可能的解决方案:

qstrcmp

数组$already_printed = Array(); $total_of_people = count($people); // while there is someone that was not printed yet while(count($already_printed) < $total_of_people){ // iterate thought each site foreach($sites as $site){ // take each person and its position in the array foreach ($people as $i => $person){ // if this i-th person was not printed yet // and is related to this $site if (! isset($already_printed[$i]) && $person['site'] == $site['acronym']){ // print this person echo 'site: '.$person['site'].'<br>'; echo 'name: '.$person['name'].'<br>'; echo '<br>'; // mark this i-th person as printed $already_printed[$i] = true; } } } } 用于了解已打印的人以避免双重打印。当它的大小等于人的总数时,我们打印每个人,所以,我们可以停止(这就是外循环所做的)。

答案 1 :(得分:1)

关键是要将大型数组组织成一种格式,这种格式不会强制您多次迭代它。试一试:

$highest = array();

foreach ($data as $person) {
    if (!array_key_exists($person['site'], $highest) || ($person['rate'] > $highest[$person['site']]['rate'])) {
        $highest[$person['site']] = $person;
    }
}

foreach ($site as $item) {
    if (array_key_exists($item['acronym'], $highest)) {
        echo "site: {$highest[$item['acronym']]['site']}<br />name:{$highest[$item['acronym']]['name']}<br />rate:{$highest[$item['acronym']]['rate']}";
    }
}

作为旁注,善意呼吁将“女孩”改为“人”,所以这并不是一个为女性评定的应用程序......

答案 2 :(得分:1)

使用你的代码我会做这样的事情:

foreach ($site as $item) {
            $maxRate = 0;
            $savePerson = array();
            foreach ($data as $person)
            {
                if($item['acronym'] == $girls['site'] && $person['site'] == $girls['site'] && $person['rate'] > $maxRate)
                {
                    $maxRate = $person['rate'];
                    $savePerson = $person;
                }
            }
           if(!empty($savePerson)){
                    echo 'name: '.$savePerson['site'].'<br>';
                    echo 'site: '.$savePerson['name'].'<br>';
                    echo '<br>';
             }
    }

我不确切地知道您是否需要这种情况 $ person [&#39; site&#39;] == $ girls [&#39; site&#39;] 。 。如果不是你可以删除它。那只是我的感受。

顺便说一下:你没有显示$ girls数组,所以我只是假设刚刚计算出来。我假设的$ data是您正在谈论的$ persons数组。