Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对数组中的最后一个值进行排序,并将已排序的数字循环到以前创建的数组中?_Php_Multidimensional Array_Ranking - Fatal编程技术网

Php 对数组中的最后一个值进行排序,并将已排序的数字循环到以前创建的数组中?

Php 对数组中的最后一个值进行排序,并将已排序的数字循环到以前创建的数组中?,php,multidimensional-array,ranking,Php,Multidimensional Array,Ranking,我需要获取多维数组中每个数组的最后一个值,相互比较,并将排序值放在先前创建的数组的末尾 我的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

我需要获取多维数组中每个数组的最后一个值,相互比较,并将排序值放在先前创建的数组的末尾

我的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++;
我需要将这些值放在正确数组的末尾,而不需要重新排列数组的顺序。下面是一个我希望在完成的数组中使用的示例

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
    )
)
我怎样才能做到这一点?我很抱歉包含这么多代码,我只是想确保我完全理解


提前非常感谢

如果我理解您的代码,这应该可以:

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

工作得很好!非常感谢你的帮助。
arsort($total_rank_array);
foreach (array_keys($total_rank_array) as $i => $key)
{
    $rsIdeas_array[$key]['total_rank'] = $i+1;
}