使用搜索表单过滤数组列表的数据

时间:2017-02-20 02:49:09

标签: php arraylist multidimensional-array

我列出了列出我所有产品的数组。样本数组:

$products = array();

$products[] = array('name'=> 'Product 1','description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 
                    'location' => 'A city', 'type' => 'Type 1', 'status' => 'new', 'tags'=>'', 'page_url'=>'p1.html', 'image'=>'products/assets/images/1/prod-image.jpg');

$products[] = array('name'=> 'Product 2','description' => 'Donec eleifend quam neque, ut mollis massa aliquet id.',
                    'location' => 'B city', 'type' => 'Type 1', 'status' => 'under', 'tags'=>'show-homepage', 'page_url'=>'p2.html', 'image'=>'products/assets/images/2/prod-image.jpg');

$products[] = array('name'=> 'Product 3','description' => 'Nam non tristique mi.',
                    'location' => 'A city', 'type' => 'Type 3', 'status' => 'new', 'tags'=>'', 'page_url'=>'p3.html', 'image'=>'products/assets/images/3/prod-image.jpg');

$products[] = array('name'=> 'Product 4','description' => 'Vestibulum accumsan dolor id orci gravida viverra.',
                    'location' => 'C city', 'type' => 'Type 2', 'status' => 'new', 'tags'=>'', 'page_url'=>'p4.html', 'image'=>'products/assets/images/4/prod-image.jpg');

我想要实现的是显示使用搜索表单过滤的所有产品。与SQL查询条件WHERE location = $_GET['location'] AND type = $_GET['type'] AND status = $_GET['status']类似。考虑到当选择的值为Any(例如位置或状态=任意)时,它将仅过滤产品的type,无论位置和状态如何,反之亦然。

这是我的html表单:

<form name="search-form" method="GET">

            <div class="searchform-title">Location</div> 
            <div style="margin-bottom:20px;">
            <select name="location" style="width:100%; padding:5px">
                 <option value='Any' selected>Any</option>
                 <option value='A city'>A city</option>
                 <option value='B City'>B City</option>
                 <option value='C city'>C city</option>
                 <option value='D city'>D city</option>
                 <option value='L city'>L city</option>
                 <option value='M city'>M city</option> 
                 <option value='T city'>T city</option>
             </select>
             </div>

            <div class="searchform-title">Type</div> 
            <div style="margin-bottom:20px;">
            <select name="type" style="width:100%; padding:5px">
                 <option value='Any' selected>Any</option>
                 <option value='Type 1'>Type 1</option>
                 <option value='Type 2'>Type 2</option>
                 <option value='Type 3'>Type 3</option>
                 <option value='Type 4'>Type 4</option>
             </select>
             </div>

              <div class="searchform-title">Status</div> 
            <div style="margin-bottom:20px;">
            <select name="status" style="width:100%; padding:5px">
                 <option value='Any' selected>Any</option>
                 <option value='New'>New</option>
                 <option value='Old'>Old</option>
                 <option value='Under'>Under</option>
             </select>
             </div>

            <input class="proj-search-btn" type="submit" name="search" value="Search"/>
        </form>

到目前为止我尝试使用的是一个函数,但它只获取产品的位置。 这是示例功能代码。 See sample code

使用搜索表单搜索数组列表中的项目有更好的方法吗?并且有3个选中的值(查询)来搜索?

1 个答案:

答案 0 :(得分:1)

最简单的方法是使用babel-node entry-point.js --preset=es2015

array_filter

结果:

<?php

$products = array();

$products[] = array('name'=> 'Product 1','description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'location' => 'A city', 'type' => 'Type 1', 'status' => 'new', 'tags'=>'', 'page_url'=>'p1.html', 'image'=>'products/assets/images/1/prod-image.jpg');

$products[] = array('name'=> 'Product 2','description' => 'Donec eleifend quam neque, ut mollis massa aliquet id.', 'location' => 'B city', 'type' => 'Type 1', 'status' => 'under', 'tags'=>'show-homepage', 'page_url'=>'p2.html', 'image'=>'products/assets/images/2/prod-image.jpg');

$products[] = array('name'=> 'Product 3','description' => 'Nam non tristique mi.', 'location' => 'A city', 'type' => 'Type 3', 'status' => 'new', 'tags'=>'', 'page_url'=>'p3.html', 'image'=>'products/assets/images/3/prod-image.jpg');

$products[] = array('name'=> 'Product 4','description' => 'Vestibulum accumsan dolor id orci gravida viverra.', 'location' => 'C city', 'type' => 'Type 2', 'status' => 'new', 'tags'=>'', 'page_url'=>'p4.html', 'image'=>'products/assets/images/4/prod-image.jpg');

function search($products, $criteria) {
    return array_filter($products, function($product) use ($criteria) {
        return array_reduce(array_keys($criteria), function($carry, $key) use ($product, $criteria) {
            return $carry && ($product[$key]===$criteria[$key]);
        }, true);
    });
}

$r = search($products, array('location'=>'A city', 'type'=>'Type 3'));
// if you don't care about keys of the array, you can skip sort
sort($r);

var_dump($r);