4

I'm trying to write a PHP function to loop into a multidimensional array to match it with a business name then return to me the "business type".

With my current skills, I wrote this function but I would like to know if there's a better solution other than looping twice because my real arrays are much bigger than the example below.

Note: I'm a student, I already searched StackOverflow but couldn't find my need.

function find_business_type($dealer_name) {
    $business_type = [
        "OEM"               => ["kia", "mercedes"],
        "Rent"              => ["rent", "rent-a-car"],
        "Workshop"          => ["shop", "workshop"],
        "Spare Parts"       => ["spare", "parts", "part"],
        "General Trading"   => ["gen", "general"]
    ];

    foreach ($business_type as $key => $values) {
        foreach ($values as $value) {
            if (strpos($dealer_name, $value) !== false) {
                return $key;
            }
        }
    }
}


$my_dealer = "super-stars-123 rent a car";
echo find_business_type($my_dealer);

Output: "Rent"

18
  • 1
    This will fall apart when a dealer name contains key-words from multiple business types. You'd better add the key to an array, complete all loops and then return the generated array. Commented Feb 14, 2018 at 13:33
  • 1
    In this case your output want to be Rent right? Commented Feb 14, 2018 at 13:33
  • 3
    to demonstrate what jeroen means: assume your dealer name is mercedes rent a car. your function will return "OEM" because it is the first match. But what about "Rent"? it would also have matched. That is why jeroen suggests returning an array that contains "OEM" and "Rent" Commented Feb 14, 2018 at 13:44
  • 1
    @Cashbee , I got it, that's a loophole caused by my dealer names and I will fix it, but still my question is there a better way/function other than looping inside a loop? Commented Feb 14, 2018 at 13:46
  • 1
    @Salam the only reliable way to improve the performance is to convert your $business_type into some structure that can be searched faster. And there are many ways to do that but in the end it cost more performance to do that if you have to do it with every search process, so you have to cache it or use a special data base depending on the size of your data and how often it changes it might not be worth so much effort. Because it will get far more complicated. Still I suggest asking the question on Code Review. :) Commented Feb 14, 2018 at 13:53

1 Answer 1

3

Here's an idea. Essentially, you can filter the array and grab all the rows that match your string based on a percent. Note that array_filter will return an array with all the matching values not just one match.

    <?php

$dealer = "super-stars-123 rent a car";

$types = [
        "OEM"               => ["kia", "mercedes"],
        "Stars"             => ["super-stars", "123"],
        "Brown"             => ["super stars", "abc"],
        "Home"              => ["think rent", "123"],
        "Super"             => ["renter", "car"],
        "Rent"              => ["rent", "rent-a-car"],
        "Workshop"          => ["shop", "workshop"],
        "Spare Parts"       => ["spare", "parts", "part"],
        "General Trading"   => ["gen", "general"]
    ];

// Filter through your array
$results = array_filter($types, function($type) use ($dealer) {
    // explode your inner array to a string and then try to match it
    // to your search dealer text. This returns a % match.
    // I would play around with this algo logic below to get it to do what you want.
    return (similar_text($dealer, implode(", ",$type), $percent) >= 8);
});

var_dump($results);   

 array (size=4)
  'Stars' => 
    array (size=2)
      0 => string 'super-stars' (length=11)
      1 => string '123' (length=3)
  'Brown' => 
    array (size=2)
      0 => string 'super stars' (length=11)
      1 => string 'abc' (length=3)
  'Super' => 
    array (size=2)
      0 => string 'renter' (length=6)
      1 => string 'car' (length=3)
  'Rent' => 
    array (size=2)
      0 => string 'rent' (length=4)
      1 => string 'rent-a-car' (length=10)
Sign up to request clarification or add additional context in comments.

1 Comment

I like similar_text but maybe there's a better way to use strpos the way you did in your question.

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.