0

Hi i have an array like below. I want to search partial data from this array. for example: i want to search "New Delhi" then i got array where city = Delhi, and search "Raigad" then got array where city = 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
        )
)
0

1 Answer 1

3

Make use of similar_text to attain this -

$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);

Works for Delhi and Raigarh.

Sign up to request clarification or add additional context in comments.

3 Comments

Didn't know this existed. But isn't it supposed to be $finalArray[$key] = $value;?
Better than using soundex() or metaphone() in this case.
yes this is okey, but not works for Raipur and Raigarh, and similar city names

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.