PHP 對數組數值進行排序,使用另一個容器,php 數組排序
PHP 對數組數值進行排序,使用另一個容器
php/* 排序方式::事實上只需要將要迴圈的數組進行N次迴圈,然後每次取最大的一個值*/$array = array(100,25,10,258,33,48,10,5,13,58,333,108,1.2,5,3,33,0.7);//3 1 2 | 1 2 | 1 |$array2 = array();for($y=1;$y<=17;$y++){ $max = 0; for($i=0;$i<count($array);$i++){ if($array[$i] > $max){ $index = $i; $max = $array[$i]; } } unset($array[$index]); sort($array); array_push($array2,$max);}print_r($array2);?>
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 "";
//轉換數組
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 '
';
print_r($stu);
echo '
';
}
//排序
$temp = sysSortArray($stu,'price',"SORT_ASC");
echo '
';
print_r($temp);
echo '
'
?>
在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 -- 使用使用者自訂的比較函數對數組中的值進行排序
http://www.bkjia.com/PHPjc/827774.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/827774.htmlTechArticlePHP 對數組數值進行排序,使用另一個容器,php 數組排序 PHP 對數組數值進行排序,使用另一個容器 ? php /* 排序方式::事實上只需要將要迴圈...