我有僱員數據存儲在一個多維數組。 現在我想通過array_multisort()
函數對數據進行排序,但我希望通過排序獲得不同的結果。 我想根據名稱或日期或數值進行排序。 下面是其中有多維數組的代碼:如何使用array_multisort()爲特定鍵排序多維數組?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ABC Company</title>
<style>
a
{
text-decoration:none; color:#000;
}
</style>
</head>
<body>
<div style="width:900px; height:500px; margin:auto; border:1px solid; text-align:center;">
<h1><strong>ABC-Company</strong></h1>
<h2>Employees:</h2>
<table width="100%" border="7">
<tr style="font-size:20px; font-weight:bold;">
<td>
<a href="#">Name</a>
</td>
<td>
<a href="#">Gender</a>
</td>
<td>
<a href="#">DOB</a>
</td>
<td>
<a href="#">Position</a>
</td>
<td>
<a href="#">Start Date</a>
</td>
<td>
<a href="#">Gross Salary</a>
</td>
<td>
<a href="#">Tax</a>
</td>
<td>
<a href="#">Pension</a>
</td>
<td>
<a href="#">Net Salary</a>
</td>
</tr>
<?php
/*****GROSS SALARY*****
Creates an array containing the gross salary data for all the employees*/
$salaries =array(635000.00, 420000.00, 260000.00, 350000.00, 250000.00, 90000.00);
/*****TAXATION*****
/* Creates if statements that runs trough the salaries and deducts tax rate accordingly for each employee*/
/* Aron and taxes*/
if ($salaries[0] >= 500000)
{$tax1=0.45;}
if ($salaries[0] >= 300000 and $salaries[0] < 500000)
{$tax1=0.35;}
if ($salaries[0] >= 200000 and $salaries[0] < 300000)
{$tax1=0.2;}
if ($salaries[0] >= 100000 and $salaries[0] < 200000)
{$tax1=0.1;}
if ($salaries[0] < 100000)
{$tax1=0;}
/* Britney and taxes*/
if ($salaries[1] >= 500000)
{$tax2=0.45;}
if ($salaries[1] >= 300000 and $salaries[1] < 500000)
{$tax2=0.35;}
if ($salaries[1] >= 200000 and $salaries[1] < 300000)
{$tax2=0.2;}
if ($salaries[1] >= 100000 and $salaries[1] < 200000)
{$tax2=0.1;}
if ($salaries[1] < 100000)
{$tax2=0;}
/* Daniel and taxes*/
if ($salaries[2] >= 500000)
{$tax3=0.45;}
if ($salaries[2] >= 300000 and $salaries[2] < 500000)
{$tax3=0.35;}
if ($salaries[2] >= 200000 and $salaries[2] < 300000)
{$tax3=0.2;}
if ($salaries[2] >= 100000 and $salaries[2] < 200000)
{$tax3=0.1;}
if ($salaries[2] < 100000)
{$tax3=0;}
/* Jessica and taxes*/
if ($salaries[3] >= 500000)
{$tax4=0.45;}
if ($salaries[3] >= 300000 and $salaries[3] < 500000)
{$tax4=0.35;}
if ($salaries[3] >= 200000 and $salaries[3] < 300000)
{$tax4=0.2;}
if ($salaries[3] >= 100000 and $salaries[3] < 200000)
{$tax4=0.1;}
if ($salaries[3] < 100000)
{$tax4=0;}
/* Peter and taxes*/
if ($salaries[4] >= 500000)
{$tax5=0.45; }
if ($salaries[4] >= 300000 and $salaries[4] < 500000)
{$tax5=0.35; }
if ($salaries[4] >= 200000 and $salaries[4] < 300000)
{$tax5=0.2; }
if ($salaries[4] >= 100000 and $salaries[4] < 200000)
{$tax5=0.1; }
if ($salaries[4] < 100000)
{$tax5=0; }
/* Keith and taxes*/
if ($salaries[5] >= 500000)
{$tax6=0.45;}
if ($salaries[5] >= 300000 and $salaries[5] < 500000)
{$tax6=0.35;}
if ($salaries[5] >= 200000 and $salaries[5] < 300000)
{$tax6=0.2;}
if ($salaries[5] >= 100000 and $salaries[5] < 200000)
{$tax6=0.1;}
if ($salaries[5] < 100000)
{$tax6=0;}
/*****PENSION*****
Creates the variabel to deduct pension*/
$pension = 0.025;
/*****ABC ARRAY*****
/*Sorts all the employees in a multiarray with their information.
It also does the tax-pension-salary*/
$abc = array(
'e_1'=>array('Aron','M','1930/01/25','Manager','1998/01/01',number_format($salaries[0],2,".",","),number_format($salaries[0]*$tax1,2,".",","),number_format($salaries[0]*$pension,2,".",","),number_format($salaries[0]-$salaries[0]*$pension-$salaries[0]*$tax1,2,".",",")),
'e_2'=>array('Britney','F','2001/05/06','Researcher','2001/03/15',number_format($salaries[1],2,".",","),number_format($salaries[1]*$tax2,2,".",","),number_format($salaries[1]*$pension,2,".",","),number_format($salaries[1]-$salaries[1]*$pension-$salaries[1]*$tax2,2,".",",")),
'e_3'=>array('Daniel','M','2003/01/15','Officer','2003/12/06',number_format($salaries[2],2,".",","),number_format($salaries[2]*$tax3,2,".",","),number_format($salaries[2]*$pension,2,".",","),number_format($salaries[2]-$salaries[2]*$pension-$salaries[2]*$tax3,2,".",",")),
'e_4'=>array('Jessica','F','2002/11/21','Officer','2007/02/20',number_format($salaries[3],2,".",","),number_format($salaries[3]*$tax4,2,".",","),number_format($salaries[3]*$pension,2,".",","),number_format($salaries[3]-$salaries[3]*$pension-$salaries[3]*$tax4,2,".",",")),
'e_5'=>array('Peter','M','1998/01/07','Assisant','2009/09/06',number_format($salaries[4],2,".",","),number_format($salaries[4]*$tax5,2,".",","),number_format($salaries[4]*$pension,2,".",","),number_format($salaries[4]-$salaries[4]*$pension-$salaries[4]*$tax5,2,".",",")),
'e_6'=>array('Keith','M','2003/07/25','Intern','2012/06/27',number_format($salaries[5],2,".",","),number_format($salaries[5]*$tax6,2,".",","),number_format($salaries[5]*$pension,2,".",","),number_format($salaries[5]-$salaries[5]*$pension-$salaries[5]*$tax6,2,".",",")));
foreach($abc as &$abct)
{
?>
<tr>
<td align="left">
<?php echo ($abct[0]);?>
</td>
<td>
<?php echo ($abct[1]);?>
</td>
<td>
<?php echo ($abct[2]);?>
</td>
<td align="left">
<?php echo ($abct[3]);?>
</td>
<td>
<?php echo ($abct[4]);?>
</td>
<td align="right">
<?php echo ($abct[5]);?>
</td>
<td align="right">
<?php echo ($abct[6]);?>
</td>
<td align="right">
<?php echo ($abct[7]);?>
</td>
<td align="right">
<?php echo ($abct[8]);?>
</td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
http://php.net/usort查看這些示例。如果你可以清楚地說明你的排序要求,你可以使用usort() –
這將是更好的,如果你會給我的陣列示例更好 @MikeB – Akmal
@Akmal爲什麼你不看手冊,併爲自己嘗試 –