在二维数组中排序列

时间:2021-08-02 20:25:37

new here so thanks for taking the time to read my question.

新来的,所以感谢您花时间阅读我的问题。

I am running some PHP code that compares the numbers enter on the screen with those in a database. The problem I am having is ordering the two dimensional array after manipulating each line. It looks as though the array id numbers are being removed. I would like to order the array by column [2] in descending order. Can anyone offer any help?

我正在运行一些PHP代码,用于将屏幕上输入的数字与数据库中的数字进行比较。我遇到的问题是在操作每一行之后对二维数组进行排序。看起来好像正在删除数组ID号。我想按列[2]按降序排序数组。有人可以提供任何帮助吗?

while( $a_row = mysql_fetch_array( $result))
    {
    $draw = array($a_row['Drawn1'], 
        $a_row['Drawn2'], 
        $a_row['Drawn3'], 
        $a_row['Drawn4'], 
        $a_row['Drawn5'], 
        $a_row['Drawn6'], 
        $a_row['Drawn7'], 
        $a_row['Drawn8']);
    $numbers = array("6", "9", "4", "8", "14", "18");
    if (count(array_intersect($draw, $numbers)) >= 1) {
        $rs = array(($a_row['DrawNo']), join(" , ",array_intersect($draw, $numbers)), count(array_intersect($draw, $numbers)));
    } else {
        $rs = null; 
    }
    array_multisort($rs[1], SORT_NUMERIC, SORT_DESC, $rs[0], SORT_ASC, SORT_STRING);
    print_r ($rs);
    echo  "<br />";
    }

This is what the output looks like.

这就是输出的样子。

Array ( [0] => A0048 [1] => 14 [2] => 1 )
Array ( [0] => A0049 [1] => 6 , 14 , 8 , 18 [2] => 4 )
Array ( [0] => A0050 [1] => 14 [2] => 1 )
Array ( [0] => A0051 [1] => 14 [2] => 1 )
Array ( [0] => A0052 [1] => 18 [2] => 1 )
Array ( [0] => A0053 [1] => 6 , 14 [2] => 2 )
Array ( [0] => A0054 [1] => 6 [2] => 1 )
Array ( [0] => A0055 [1] => 14 [2] => 1 )
Array ( [0] => A0056 [1] => 4 [2] => 1 )
Array ( [0] => A0057 [1] => 9 , 6 , 4 [2] => 3 )

Thanks for your time
zeroanarchy

谢谢你的时间零度

2 个解决方案

#1


Numeric array keys being reindexed is part of the documented behavior of array_multisort(). If you need keys preserved, you need to convert them to string keys.

重新索引的数字数组键是array_multisort()记录的行为的一部分。如果需要保留密钥,则需要将它们转换为字符串密钥。

#2


you are probably looking for: http://php.net/manual/en/function.array-multisort.php

你可能正在寻找:http://php.net/manual/en/function.array-multisort.php

#1


Numeric array keys being reindexed is part of the documented behavior of array_multisort(). If you need keys preserved, you need to convert them to string keys.

重新索引的数字数组键是array_multisort()记录的行为的一部分。如果需要保留密钥,则需要将它们转换为字符串密钥。

#2


you are probably looking for: http://php.net/manual/en/function.array-multisort.php

你可能正在寻找:http://php.net/manual/en/function.array-multisort.php