Table of Contents
PHP 对数组数值进行排序,使用另一个容器,php 数组排序
PHP 对数组数值进行排序,使用另一个容器
1使用PHP的排序函数对以下中的数组排序 2在表单上由用户输入学号,姓名与成绩这三列的数据,一共5行
在php中怎排序键值数组
Home php教程 php手册 PHP 对数组数值进行排序,使用另一个容器,php 数组排序

PHP 对数组数值进行排序,使用另一个容器,php 数组排序

Jun 13, 2016 am 09:29 AM
php use container right sort numerical value array conduct

PHP 对数组数值进行排序,使用另一个容器,php 数组排序

PHP 对数组数值进行排序,使用另一个容器

<?<span>php
</span><span>/*</span><span> 排序方式::事实上只需要将要循环的数组进行N次循环,然后每次取最大的一个值</span><span>*/</span>
<span>$array</span> = <span>array</span>(100,25,10,258,33,48,10,5,13,58,333,108,1.2,5,3,33,0.7);<span>//</span><span>3 1 2 | 1 2 | 1 |</span>
<span>$array2</span> = <span>array</span><span>();
</span><span>for</span>(<span>$y</span>=1;<span>$y</span><=17;<span>$y</span>++<span>){
    </span><span>$max</span>   = 0<span>;
    </span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>count</span>(<span>$array</span>);<span>$i</span>++<span>){
        </span><span>if</span>(<span>$array</span>[<span>$i</span>] > <span>$max</span><span>){
            </span><span>$index</span> = <span>$i</span><span>;
            </span><span>$max</span> = <span>$array</span>[<span>$i</span><span>];
        }
    }
    </span><span>unset</span>(<span>$array</span>[<span>$index</span><span>]);
    </span><span>sort</span>(<span>$array</span><span>);
    </span><span>array_push</span>(<span>$array2</span>,<span>$max</span><span>);
}
</span><span>print_r</span>(<span>$array2</span><span>);
</span>?>
Copy after login


1使用PHP的排序函数对以下中的数组排序 2在表单上由用户输入学号,姓名与成绩这三列的数据,一共5行

费了一段时间,总算完成这个作业,也算是一种锻炼吧,以下是代码,虽然感觉效率比较低,为什么不用数据库呢?


header('Content-type:text/html;charset=utf-8');
//2维数组排序
function sysSortArray($ArrayData, $KeyName1, $SortOrder1 = "SORT_ASC", $SortType1 = "SORT_REGULAR")
{
if (!is_array($ArrayData)) {
return $ArrayData;
}

// Get args number.
$ArgCount = func_num_args();

// Get keys to sort by and put them to SortRule array.
for ($I = 1; $I < $ArgCount; $I++) {
$Arg = func_get_arg($I);
if (!eregi("SORT", $Arg)) {
$KeyNameList[] = $Arg;
$SortRule[] = '$' . $Arg;
} else {
$SortRule[] = $Arg;
}
}

// Get the values according to the keys and put them to array.
foreach ($ArrayData as $Key => $Info) {
foreach ($KeyNameList as $KeyName) {
${$KeyName}[$Key] = $Info[$KeyName];
}
}

// Create the eval string and eval it.
$EvalString = 'array_multisort(' . join(",", $SortRule) . ',$ArrayData);';
eval($EvalString);
return $ArrayData;
}

$keys = array('stu_no','name','price');

//输出表格
echo "请输入需要排序的数据:
";
echo "
";
echo "";
echo "";
for ($row = 1; $row < 6; $row++) {
echo "";
for ($col = 1; $col < 4; $col++) {
echo "";
}
echo "";
}
echo "
学号姓名成绩
";
echo "";
echo "
";

//转换数组
if (isset($_POST['bt'])) {
for ($row = 1; $row < 6; $row++)
for ($col = 1; $col < 4; $col++) {
$key = $keys[$col-1];
$stu[$row][$key] = $_POST['stu_' . $row . '_' . $col];
}
echo '
&#39;;<br />   print_r($stu);<br />   echo &#39;
Copy after login
';

}

//排序
$temp = sysSortArray($stu,'price',"SORT_ASC");

echo '
&#39;;<br />print_r($temp);<br />echo &#39;
Copy after login
'

?>

在php中怎排序键值数组

array_change_key_case -- 返回字符串键名全为小写或大写的数组
array_chunk -- 将一个数组分割成多个
array_combine -- 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值
array_count_values -- 统计数组中所有的值出现的次数
array_diff_assoc -- 带索引检查计算数组的差集
array_diff_uassoc -- Computes the difference of arrays with additional index check which is performed by a user supplied callback function.
array_diff -- 计算数组的差集
array_fill -- 用给定的值填充数组
array_filter -- 用回调函数过滤数组中的单元
array_flip -- 交换数组中的键和值
array_intersect_assoc -- 带索引检查计算数组的交集
array_intersect -- 计算数组的交集
array_key_exists -- 检查给定的键名或索引是否存在于数组中
array_keys -- 返回数组中所有的键名
array_map -- 将回调函数作用到给定数组的单元上
array_merge_recursive -- 递归地合并两个或多个数组
array_merge -- 合并两个或多个数组
array_multisort -- 对多个数组或多维数组进行排序
array_pad -- 用值将数组填补到指定长度
array_pop -- 将数组最后一个单元弹出(出栈)
array_push -- 将一个或多个单元压入数组的末尾(入栈)
array_rand -- 从数组中随机取出一个或多个单元
array_reduce -- 用回调函数迭代地将数组简化为单一的值
array_reverse -- 返回一个单元顺序相反的数组
array_search -- 在数组中搜索给定的值,如果成功则返回相应的键名
array_shift -- 将数组开头的单元移出数组
array_slice -- 从数组中取出一段
array_splice -- 把数组中的一部分去掉并用其它值取代
array_sum -- 计算数组中所有值的和
array_udiff_assoc -- Computes the difference of arrays with additional index check. The data is compared by using a callback function.
array_udiff_uassoc -- Computes the difference of arrays with additional index check. The data is compared by using a callback function. The index check is done by a callback function also
array_udiff -- Computes the difference of arrays by using a callback function for data comparison.
array_unique -- 移除数组中重复的值
array_unshift -- 在数组开头插入一个或多个单元
array_values -- 返回数组中所有的值
array_walk -- 对数组中的每个成员应用用户函数
array -- 新建一个数组
arsort -- 对数组进行逆向排序并保持索引关系
asort -- 对数组进行排序并保持索引关系
compact -- 建立一个数组,包括变量名和它们的值
count -- 统计变量中的单元数目
current -- 返回数组中的当前单元
each -- 返回数组中当前的键/值对并将数组指针向前移动一步
end -- 将数组的内部指针指向最后一个单元
extract -- 从数组中将变量导入到当前的符号表
in_array -- 检查数组中是否存在某个值
key -- 从结合数组中取得键名
krsort -- 对数组按照键名逆向排序
ksort -- 对数组按照键名排序
list -- 把数组中的值赋给一些变量
natcasesort -- 用“自然排序”算法对数组进行不区分大小写字母的排序
natsort -- 用“自然排序”算法对数组排序
next -- 将数组中的内部指针向前移动一位
pos -- 得到数组当前的单元
prev -- 将数组的内部指针倒回一位
range -- 建立一个包含指定范围单元的数组
reset -- 将数组的内部指针指向第一个单元
rsort -- 对数组逆向排序
shuffle -- 将数组打乱
sizeof -- count() 的别名
sort -- 对数组排序
uasort -- 使用用户自定义的比较函数对数组中的值进行排序并保持索引关联
uksort -- 使用用户自定义的比较函数对数组中的键名进行排序
usort -- 使用用户自定义的比较函数对数组中的值进行排序
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles