"lemon", "a"=>"orange"/> "lemon", "a"=>"orange">

二维数组排序,且上标重新排

WBOY
Release: 2016-06-13 10:48:48
Original
711 people have browsed it

二维数组排序,且下标重新排
如题,我对某字段排序,排好后,发现下标也跟着走,希望下标重新排,从0~开始排,问怎么做?

------解决方案--------------------

PHP code
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");ksort($fruits);print "<pre class="brush:php;toolbar:false">";var_dump($fruits);<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
PHP code
$ar = array(       array("10", 11, 100, 100, "a"),       array(   1,  2, "2",   3,   1)      );   array_multisort($ar[0], SORT_ASC, SORT_STRING,                $ar[1], SORT_NUMERIC, SORT_DESC);//SORT_ASC, SORT_DESC, SORT_REGULAR,SORT_NUMERIC, SORT_STRINGprint "<pre class="brush:php;toolbar:false">";                print_r($ar);<br><font color="#e78608">------解决方案--------------------</font><br>我想你需要这个——<br>看清楚最后一段代码的最后一个参数<br><br><br>Example #4 对数据库结果进行排序<br><br><br>  本例中 data数组中的每个单元表示一个表中的一行。这是典型的数据库记录的数据集合。  <br><br><br>例子中的数据如下:  <br><br><br>volume | edition<br>-------+--------<br>   67 |       2<br>   86 |       1<br>   85 |       6<br>   98 |       2<br>   86 |       6<br>   67 |       7<br><br><br>  数据全都存放在名为 data的数组中。这通常是通过循环从数据库取得的结果,例如 mysql_fetch_assoc()。  <br><br><br><?php <br />$data[] = array('volume' => 67, 'edition' => 2);<br>$data[] = array('volume' => 86, 'edition' => 1);<br>$data[] = array('volume' => 85, 'edition' => 6);<br>$data[] = array('volume' => 98, 'edition' => 2);<br>$data[] = array('volume' => 86, 'edition' => 6);<br>$data[] = array('volume' => 67, 'edition' => 7);<br>?>  <br><br><br>本例中将把 volume 降序排列,把 edition 升序排列。  <br><br><br>现在有了包含有行的数组,但是 array_multisort()需要一个包含列的数组,因此用以下代码来取得列,然后排序。  <br><br><br><?php <br />// 取得列的列表<br>foreach ($data as $key => $row) {<br>   $volume[$key]  = $row['volume'];<br>   $edition[$key] = $row['edition'];<br>}<br><br>// 将数据根据 volume 降序排列,根据 edition 升序排列<br>// 把 $data 作为最后一个参数,以通用键排序<br>array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);<br>?>  <br><br><br>数据集合现在排好序了,结果如下:  <br><br><br>volume | edition<br>-------+--------<br>   98 |       2<br>   86 |       1<br>   86 |       6<br>   85 |       6<br>   67 |       2<br>   67 |       7<br><br><br><br><font color="#e78608">------解决方案--------------------</font><br>设 $t 是排序的结果,下标不是升序的<br>则 $t = array_values($t);<br>就可化为下标升序的了<div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!