无法绕过array_filter()

时间:2017-11-05 15:50:24

标签: php html arrays array-filter

<html>
<style>
h1 {
    text-align: center;
}
div {
    text-align: center;
    border: 1px solid;
    padding: 20px;
    width: 520px;
    margin: 0 auto;
}

table{
    text-align: center;
    border: 1px solid;
    padding: 20px;
    width: 520px;
    margin: 0 auto;
}
</style>

<div>
<h1>Students By Major</h1>
<br>
<?PHP

$students = array(
            array('name' => 'Rex Solertan', 'hrs_attempted' => 23, 'gpa_points' => 28, 'major' => 'CIT'),
            array('name' => 'Rivka Alexander', 'hrs_attempted' => 41, 'gpa_points' => 103, 'major' => 'EET'),
            array('name' => 'Ned Ramos', 'hrs_attempted' => 28, 'gpa_points' => 52, 'major' => 'BUS'),
            array('name' => 'Zoe Martinez', 'hrs_attempted' => 30, 'gpa_points' => 93, 'major' => 'WEB'),
            array('name' => 'Carl McElan', 'hrs_attempted' => 54, 'gpa_points' => 100, 'major' => 'WEB'),
            array('name' => 'Bob Palla', 'hrs_attempted' => 21, 'gpa_points' => 36, 'major' => 'CIT'),
            array('name' => 'Tilda Downey', 'hrs_attempted' => 31, 'gpa_points' => 86, 'major' => 'CIT'),
            array('name' => 'Mary Proth', 'hrs_attempted' => 33, 'gpa_points' => 105, 'major' => 'CIT'),
            array('name' => 'Zelda Rowe', 'hrs_attempted' => 12, 'gpa_points' => 39, 'major' => 'BUS'),
            array('name' => 'Sue Mar', 'hrs_attempted' => 20, 'gpa_points' => 50, 'major' => 'BUS'),
            array('name' => 'Max Stone', 'hrs_attempted' => 42, 'gpa_points' => 80, 'major' => 'EET'));


//**** NEED TO FIGURE OUT FILTER TO DISPLAY ONLY STUDENTS WITH CERTAIN MAJOR FOR EACH SELECT ITEM BELOW ****

if (isset($_POST["business"])) {


}
if (isset($_POST["computer_information_technology"])) {


}

if (isset($_POST["electrical_engineering_technology"])) {


}

if (isset($_POST["web_site_design"])) {


}

//WORKING CODE TO DISPLAY ALL STUDENTS NAME, GPA AND MAJOR (PROGRAM WILL RUN AS EXPECTED BUT ONLY DISPLAY REQUESTED DATA FOR ENTIRE ARRAY NOT SPECIFIC MAJORS)

echo "<table border ='1' cellspacing='0' cellpadding='3'> \n";
    echo "<tr><td>Name</td><td>GPA</td><td>Major</td></tr>\n";

$length = count($students);

    for ($i = 0; $i < $length; $i++) {

        $points = $students[$i]['gpa_points'];
        $hours = $students[$i]['hrs_attempted'];
        $GPAz = ($points / $hours);
        $GPA = round($GPAz, 1);
        $Major = $students[$i]['major'];
        $Name = $students[$i]['name'];

        echo "<tr><td>$Name</td><td>$GPA</td><td>$Major</td></tr>";


}

?>
</html>

这是一个项目,我不是要求得到答案。我已经完成了很多项目,但是我只需要创建一个过滤器,只在从我的HTML表单中选择与该元素相关的选择项时才显示某些数组元素(主要)。

我已经想出如何显示整个数组的请求数据,我一直在研究array_filter()函数,但我不明白如何将数据输入到它中。

我假设我可以创建过滤器函数并使用我已经创建的代码来显示我想要的数组元素,或者也必须重写这些元素?

任何人都可以启发我吗?

2 个答案:

答案 0 :(得分:1)

由于您已经在对数组进行迭代,我建议使用in_array()函数和其他变量$filter

来解决您的问题。

首先创建一个数组$filter,并显示 majors

$filter = array();

if (isset($_POST["business"])) {
    $filter[] = "BUS";
}
if (isset($_POST["computer_information_technology"])) {
    $filter[] = "CIT";
}

if (isset($_POST["electrical_engineering_technology"])) {
    $filter[] = "EET";
}

if (isset($_POST["web_site_design"])) {
    $filter[] = "WEB";
}

然后在for循环内部仅在数组$Major中存在$filter时才显示该元素:

if( in_array( $Major, $filter ) ) {
    echo "<tr><td>$Name</td><td>$GPA</td><td>$Major</td></tr>";
}

参考:

<强> in_array()

答案 1 :(得分:1)

一般解决方案:

// lookup array for majors
$major_lookup = [
    "business" => 'BUS',
    "computer_information_technology" => 'CIT',
    "electrical_engineering_technology" =>  'EET',
    "web_site_design" => 'WEB',
];
// your intial students array
$students = []; 

$major_filter = $_POST['major_filter'] ?: '';

// if `$major_filter` key presents in 
// `$major_lookup`, then filter by it's value:
if (isset($major_lookup[$major_filter])) {
    $major = $major_lookup[$major_filter];
    $students = array_filter(
        $students,
        function($v) use ($major) {
            return $v['major'] == $major;
        }
    );
}

// output $students as usual