2015-11-07 44 views
1

如何在PHP中基於另一個數組對數組進行排序?我個人的這個名單,其實他們是1300 +的人......我只在這裏顯示22人:)如何排序基於從另一個數組在PHP中的數組?

人陣:

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) 
) 

並且存在另一個數組稱爲位點我想相應地顯示它們,每個循環每個站點只有一個人,並且當站點數組到達結尾時,它將開始到頂部,直到人員列表完成讀取。

現場秩序陣列

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 

注意,我希望有一個條件,即: 環路人員命令由現場下單和訂單通過但每個站點循環只有一個人。

我知道這聽起來有點複雜,但請幫助我。我在這裏搜索了很多線程,與我有同樣的問題,但沒有運氣。 :'(

+1

你能添加你想要的輸出嗎? – roullie

+0

是的確定@roullie等一下 – xerwudjohn

+0

@roullie你有什麼想法?請幫助我。嘿嘿,我已經編輯我的問題 – xerwudjohn

回答

2

下面的代碼是一個可能的解決方案:

$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; 
      } 

     } 
    } 
} 

的陣列already_printed用於知道誰已經打印以避免雙重打印。當它的大小等於人的總數時,那麼,我們印出每個人,所以,我們可以停止(這是外層循環所做的)。

+0

是的,我認爲應該這樣做。但我認爲這個代碼foreach($ site爲$ site)'必須是'foreach($ site爲$ sites)'對嗎? – xerwudjohn

+0

嘿芽@vitor你能解釋我是如何工作的嗎? '$ person = $ people [$ i];'是直接在上面的'persons = array()'上調用的嗎? – xerwudjohn

+0

即時通訊在這裏有一個錯誤,它說'$ data = $ data [$ i]'未定義偏移量:1';' – xerwudjohn

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']}"; 
    } 
} 

作爲一個側面說明,在不斷變化的「女孩」到「人」良好的通話,所以這是不那麼明顯的創建率女性應用...

+0

,但我想每個站點循環只顯示一個人。並且它必須按照速度順序排序@rawb – xerwudjohn

+0

嗯,我在這裏有1300多個人,但它似乎並沒有顯示所有列表。呵呵。但我認爲我們到了那裏。 – xerwudjohn

+1

這是我的理解,你只是想每個網站顯示最高評分的人,但無論哪種方式,這應該指向你在正確的方向!類似於上面的方法 - 我建議先按網站排序(在由網站索引的多維數組中),然後按照評級排序每個這些站點子數組,然後循環遍歷整個事物(當存在仍然有人離開)並回應你需要的東西。祝你好運,快樂的編碼! – rawb

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>'; 
      } 
    } 

我並不確切地知道,如果你需要這個條件$人[「現場」] == $女孩[「現場」] ..如果你不能刪除它。那只是我的感覺。

btw:你沒有顯示$女孩陣列,所以我只是假設只是以前計算。我假設的$ data是你正在談論的$ persons數組。

+0

對不起,女孩是人的陣列。我編輯了我的問題,以確保清楚。 :) – xerwudjohn

相關問題