如何使用array_multisort()为特定键对多维数组进行排序?

时间:2013-10-04 17:16:47

标签: php arrays sorting multidimensional-array

我将员工数据存储在多维数组中。 现在我想通过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>

1 个答案:

答案 0 :(得分:0)

您可能希望更改$ abc数组,以便每个子数组也是一个关联数组,这样可以用于排序的键被指定为更容易对数字索引有意义的字符串,如下所示:这个简单的数据示例:

$abc = array(   'rec_1' => array('fn'=>'Aron','ln'=>'Michaelson'),
                'rec_2' => array('fn'=>'Abbey','ln'=>'Drake'),
                'rec_3' => array('fn'=>'Alice','ln'=>'Peterson'));
因此,$ abc是一个关联数组,其子数组具有的键很容易通过firstname(&#39; fn&#39;)或lastname(&#39; ln&#39;)对数据进行排序

一种优雅的排序方式,例如使用lastname,是使用闭包,如下所示:

function build_sorter($key) {
    return function ($a, $b) use ($key) {
        return strnatcmp($a[$key], $b[$key]);
    };
}

usort($abc, build_sorter('ln'));

build_sorter将一个键作为参数,该参数将是函数返回的闭包的绑定变量。 usort将使用该闭包,该闭包表示一个比较函数,其返回值是一个整数,表示被比较的值是相同的还是一个大于另一个。

结果可以通过以下代码获得:

foreach ($abc as $item) {
    echo strtoupper($item['ln']) . ', ' . $item['fn'] . "\n";
}

// output:

DRAKE, Abbey
MICHAELSON, Aron
PETERSON, Alice

如果出于某种原因,您宁愿使用带有名称的函数,那么使用这种代码也可以获得相同的结果:

function SortByLname($a,$b) {
    return strnatcmp( $a['ln'], $b['ln'] );
}
usort($abc, "SortByLname");


foreach ($abc as $item) {
    echo strtoupper($item['ln']) . ', ' . $item['fn'] . "\n";
}

注意:strnatcmp有助于处理可能包含数字字符串的数据,并且比较将更符合我们对人类订购此类值的方式的期望。您可以在http://3v4l.org/fng8hhttp://3v4l.org/QYt9S

上查看这些示例