二维数组排序,且上标重新排
二维数组排序,且下标重新排
如题,我对某字段排序,排好后,发现下标也跟着走,希望下标重新排,从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>
- 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_STRING print "<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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to Use macOS Recovery to Install macOS on a New SSD On any Mac built after 2009, there is a built-in recovery system. This allows you to boot your Mac into recovery mode. In this mode, you can repair the internal disk, restore files from a Time Machine backup, get online help, or reinstall macOS. You must have an Internet connection to use these tools. You can use the macOS installation tool to install macOS on a new SSD installed in your computer. To use Internet Recovery to install macOS on an SSD: Follow the manufacturer's instructions

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

1. Introduction to LinuxLVM LinuxLogicalVolumeManager (LVM) is a tool for managing disks and storage space. It provides flexible storage management through volume groups and logical volumes. The core concepts of LVM include physical volumes, volume groups and logical volumes. Physical volume (PhysicalVolume, PV): A physical volume is a physical hard disk or partition, which is used by LVM to store data. LVM combines one or more physical volumes into volume groups. Volume Group (VG): A volume group is a logical storage unit composed of one or more physical volumes. Logical volumes are created on volume groups, and they can dynamically allocate and reclaim storage space. A system can contain multiple volume groups. logical volume (

The data folder contains system and program data, such as software settings and installation packages. Each folder in the Data folder represents a different type of data storage folder, regardless of whether the Data file refers to the file name Data or the extension. Named data, they are all data files customized by the system or program. Data is a backup file for data storage. Generally, it can be opened with meidaplayer, notepad or word.

In PHP programming, array is a very important data structure that can handle large amounts of data easily. PHP provides many array-related functions, array_fill() is one of them. This article will introduce in detail the usage of the array_fill() function, as well as some tips in practical applications. 1. Overview of the array_fill() function The function of the array_fill() function is to create an array of a specified length and composed of the same values. Specifically, the syntax of this function is
