在多维数组中进行部分搜索

时间:2018-02-23 11:25:48

标签: php arrays search multidimensional-array

嗨我有一个像下面这样的数组。我想从这个数组中搜索部分数据。 例如:我想搜索“新德里”,然后我得到城市=德里的阵列,搜索“Raigad”然后得到阵列城市= Raigarh

Array(
    [56] => Array
        (
            [city] => Davangere
            [product_id] => 14
            [tier] => Tier 4
        )

    [57] => Array
        (
            [city] => Dehradun
            [product_id] => 14
            [tier] => Tier 3
        )

    [58] => Array
        (
            [city] => Delhi
            [product_id] => 14
            [tier] => Metro
        )

    [59] => Array
        (
            [city] => Delhi
            [product_id] => 14
            [tier] => Metro
        )
    [60] => Array
        (
            [city] => Raigarh
            [product_id] => 14
            [tier] => Metro
        )
)

1 个答案:

答案 0 :(得分:3)

利用similar_text来实现这一目标 -

$finalArray = array();
$searchString = "New Delhi";

//Loop through your array
foreach ($your_array as $key => $value) {
  similar_text($searchString, $value['city'], $percentageSimilarity);
  //if percentage similarity between the text is above 70%, add to to our final array
  if ($percentageSimilarity > 70) {
    $finalArray[$key] = $value;
  }
}

var_dump($finalArray);

为德里和Raigarh工作。