简体   繁体   English

搜索PHP数组值并显示它们(如果存在)

[英]Search PHP Array Values and Display them if they exist

I have two pages, an HTML, and PHP page. 我有两个页面,一个HTML和PHP页面。 The HTML contains a form that asks a user to type a City name and how they want to sort it (none, ascending, or descending). HTML包含一个表格,要求用户输入城市名称以及他们如何对其进行排序(无,升序或降序)。 The PHP page receives the input and contains an array of cities. PHP页面接收输入,并包含一个城市数组。 My question is: how can I display the values (using a loop) that exist in the array if the input contains part of the string? 我的问题是:如果输入包含字符串的一部分,如何显示数组中存在的值(使用循环)? Ex: if I search "De", it should return Detroit and Dearborn and sort them based on how I choose. 例如:如果我搜索“ De”,它将返回底特律和迪尔伯恩,并根据我的选择对它们进行排序。

Here is the php code: 这是PHP代码:

<?php
        $sort = substr(filter_input(INPUT_GET, 'sort', FILTER_SANITIZE_STRING),0,4);
        $city = substr(filter_input(INPUT_GET, 'city', FILTER_SANITIZE_STRING),0,20);
          $cities[0] = "Detroit, Michigan";
          $cities[1] = "Rome, Italy";
          $cities[2] = "New York, New York";
          $cities[3] = "Austin, Texas";
          $cities[4] = "Atlanta, Georgia";
          $cities[5] = "Dubai, UAE";
          $cities[6] = "Shanghai, China";
          $cities[7] = "Mumbai, India";
          $cities[8] = "Windsor, Canada";
          $cities[9] = "Los Angeles, California";
          $cities[10] = "Chicago, Illinois";
          $cities[11] = "London, England";
          $cities[12] = "Dearborn, Michigan";
          $cities[13] = "Istanbul, Turkey";
          $cities[14] = "Pittsburgh, Pennsylvania";
          $cities[15] = "Cincinnati, Ohio";
          $cities[16] = "Anaheim, California";
          $cities[17] = "Orlando, Florida";
          $cities[18] = "Dallas, Texas";
          $cities[19] = "Hong Kong, China";
          $cities[20] = "Cairo, Egypt";
?>

array_filter() with a fnmatch() check seems a logical approach: 带有fnmatch()检查的array_filter()似乎是一种逻辑方法:

$matches = array_filter(
    $cities,
    function($value) use ($city) {
        return fnmatch('*'.$city.'*', $value, FNM_CASEFOLD);
    }
);

EDIT 编辑

Explanation... array_filter() executes the callback against each value in the array ($cities) and that callback returns a boolean true/false to indicate whether it should be accepted or discarded. 说明... array_filter()对数组($ cities)中的每个值执行回调,并且该回调返回布尔值true / false,以指示是否应接受或丢弃该值。 The callback uses the fnmatch() function to execute a wildcard comparison against the array entry value against your search value; 回调使用fnmatch()函数对数组条目值和搜索值执行通配符比较; if the value matches, then it returns a true; 如果值匹配,则返回true;否则,返回true。 else it returns a false. 否则返回假。

The result; 结果; the $matches array contains only those entries that match your search criteria. $ matches数组仅包含那些符合搜索条件的条目。

Demo 演示

If you want the result ($matches) sorted; 如果要对结果($ matches)进行排序; then sort it after running that filter 然后在运行该过滤器后对其进行排序

First, you can filter your cities with array_filter . 首先,您可以使用array_filter过滤城市。

$filtered = array_filter($cities, function($current) use ($city) {
    // If the current city start with our input, we keep it.
    return stripos($current, $city) === 0;
});

Second, you can sort your cities with sort rsort depending of the order. 其次,你可以用你的城市排序sort rsort取决于订单的。

if ($sort === 'ASC') {
    sort($filtered);
} elseif ($sort === 'DESC') {
    rsort($filtered);
}

And then, you just have to return your filtered and ordered results. 然后,您只需要返回经过过滤和排序的结果。

You could loop over the array and use the PHP strstr function to find matches: 您可以遍历数组,并使用PHP strstr函数查找匹配项:

$matches = array();
foreach ($cities as $city) {
    if (strstr($city, $search)) {
        $matches[] = $city;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM