2013-02-10 46 views
0

我需要採取多維數組中的每個數組的最後一個值,將它們相互比較,並將排序的值放在先前創建的數組的末尾。將數組中的最後值排在先前創建的數組中的排列數?

我的PHP:

//Arrays 
$rsIdeas_array = array(); 
$total_rank_array = array(); 

//Get Data 
//MySQL Select Queries Here 
//loop bizideas into array 
do { 
//Calculate variables needed 
$search_rank = round(($row_rsIdeas['monthlysearches']/$row_rsMaxSearches['monthlysearches'] * 9) + 1, 1, PHP_ROUND_HALF_EVEN); 
$competition_rank = round(10 - (($row_rsIdeas['advcomp']/($row_rsMaxCompetition['advcomp']) * 9)), 1, PHP_ROUND_HALF_EVEN); 
$search_competition_ease = ($search_rank + $competition_rank + $row_rsIdeas['startease']); 
$row_rsIdeas['search_rank'] = $search_rank; 
$row_rsIdeas['competition_rank'] = $competition_rank; 
$row_rsIdeas['search_competition_ease'] = $search_competition_ease; 

$row_rsIdeas = array(
    'ideaID' => $row_rsIdeas['ideaID'], 
    'userID' => $row_rsIdeas['userID'], 
    'bizidea' => $row_rsIdeas['bizidea'], 
    'bizexplained' => $row_rsIdeas['bizexplained'], 
    'bizmodel' => $row_rsIdeas['bizmodel'], 
    'repkeyword' => $row_rsIdeas['repkeyword'], 
    'monthlysearches' => $row_rsIdeas['monthlysearches'], 
    'search_rank' => $row_rsIdeas['search_rank'], 
    'advcomp' => $row_rsIdeas['advcomp'], 
    'competition_rank' => $row_rsIdeas['competition_rank'], 
    'search_competition_ease' => $row_rsIdeas['search_competition_ease'], 
); 
array_push($rsIdeas_array, $row_rsIdeas); 
array_push($total_rank_array, $search_competition_ease); 
} while ($row_rsIdeas = mysql_fetch_assoc($rsIdeas)); 

這將產生

Array 
(
[0] => Array 
    (
     [ideaID] => 1 
     [userID] => 1 
     [bizidea] => Business Idea 1 
     [bizexplained] => Business Idea 1 Explanation 
     [bizmodel] => Utility 
     [repkeyword] => Business Idea 1 Keyword 
     [monthlysearches] => 33100 
     [search_rank] => 1 
     [advcomp] => 0.95 
     [competition_rank] => 1.1 
     [search_competition_ease] => 8.1 //Value to Rank From 
    ) 

[1] => Array 
    (
     [ideaID] => 2 
     [userID] => 1 
     [bizidea] => Business Idea 2 
     [bizexplained] => Business Idea 2 Explained 
     [bizmodel] => Service 
     [repkeyword] => Business 2 Keyword 
     [monthlysearches] => 6600 
     [search_rank] => 1 
     [advcomp] => 0.93 
     [competition_rank] => 1.3 
     [search_competition_ease] => 10.3 //Value to Rank From 
    ) 
) 

我有一個產生的排名一節:

arsort($total_rank_array); 
$rank = 1; 
foreach($total_rank_array as $ideaId => $value) { 
print('Rank: ' . $rank . '<br />Value: ' . $value . '<br />'); 
$rank++; 

它創建(數字越高,更好的等級):

Rank: 1 
Value: 10.3 
Rank: 2 
Value: 8.1 

我需要把這些值放在正確的數組的末尾而不重新排列數組的順序。這是我想要完成的數組中的一個示例。

Array 
(
[0] => Array 
    (
     [ideaID] => 1 
     [userID] => 1 
     [bizidea] => Business Idea 1 
     [bizexplained] => Business Idea 1 Explanation 
     [bizmodel] => Utility 
     [repkeyword] => Business Idea 1 Keyword 
     [monthlysearches] => 33100 
     [search_rank] => 1 
     [advcomp] => 0.95 
     [competition_rank] => 1.1 
     [search_competition_ease] => 8.1 
     [total_rank] => 2 //Here is where I need the additional value 
    ) 

[1] => Array 
    (
     [ideaID] => 2 
     [userID] => 1 
     [bizidea] => Business Idea 2 
     [bizexplained] => Business Idea 2 Explained 
     [bizmodel] => Service 
     [repkeyword] => Business 2 Keyword 
     [monthlysearches] => 6600 
     [search_rank] => 1 
     [advcomp] => 0.93 
     [competition_rank] => 1.3 
     [search_competition_ease] => 10.3 
     [total_rank] => 1 //Here is where I need the additional value 
    ) 
) 

我該如何做到這一點?我很抱歉包含這麼多的代碼,我只是想確保我完全理解。

非常感謝您提前!

回答

0

如果我理解你的代碼,這應該工作:

arsort($total_rank_array); 
foreach (array_keys($total_rank_array) as $i => $key) 
{ 
    $rsIdeas_array[$key]['total_rank'] = $i+1; 
} 
+0

完美地工作!非常感謝您的幫助。 – KevBot 2013-02-10 00:49:56