0

I have this array sample:

array(3) { 
[0]=> string(20) "1845-260-phone-nokia"
[1]=> string(22) "1133-0-phone-motorola" 
[2]=> string(20) "1133-0-phone-samsung" 
}

My goal is to search the array and to find if the string exists in the array.

Let me first explain what this string means:

"1845-260-phone-nokia"

1845 - product id 260 - this is variable and it is unknown phone - is product make nokia is product model All is known excluding the variable..

How can I search the array so if I have:

[0]=> string(20) "18451-260-phone-nokia"

to be recognized as different product I hope you will undestand what I mean. Till now I used in_array but after recently added variable in the string this doesnt work.. Thank you for helping me !

17
  • for example, try with the manual: php.net/manual/en/function.array-search.php Commented Feb 5, 2015 at 10:31
  • Do you want to search for the whole string or just some part of it? Commented Feb 5, 2015 at 10:33
  • array-search will search for exact string in my question I dont know what it would be because of the variable.. Commented Feb 5, 2015 at 10:33
  • array_search() is looking for an exact match.... but using array_filter() with a callback using fnmatch() or even simply strpos() should work Commented Feb 5, 2015 at 10:34
  • Do u want to search the whole string for the product id and display the corresponding product? Commented Feb 5, 2015 at 10:35

2 Answers 2

1

While trying to fulfill your wish that no explode() function calls are used, I have created two functions that can match using the model number and the manufacturer. The first function matches using a regular expression, while the second using string manipulation. Performance wise I have no idea. They could properly both be optimized A LOT, but the functionality should be there.

The regular expression version has the advantage that it captures the unknown number. This can easily be refactored away if you do not want that functionality. But my guess is that it is somehow important. If not, then why store it in the first place :D

/**
 * Finds the first matching phone using a regular expression.
 *
 * @param array  $phones        An array of available phones
 * @param string $model         The model number of the phone
 * @param string $manufacturer  The manufacturer of the phone
 *
 * @return array|bool Returns an associative array with matched phone and the
 *                    unknown number on success. Returns FALSE if no match.
 */
function find_phone_regex(array $phones, $model, $manufacturer) {

    /*
     * OPS: I have added the 'i' flag to make it case-insensitive.
     * This might not be the wished behavior.
     */
    $regex = '~^' . $model . '-(?<number>[0-9]+)-phone-' . $manufacturer . '$~i';

    foreach($phones as $phone) {

        if(preg_match($regex, $phone, $matches)) {

            return [
                'phone'  => $phone,
                'number' => $matches['number']
            ];

        }
    }

    return false;
}

/**
 * Finds the first matching phone.
 *
 * @param array  $phones        An array of available phones
 * @param string $model         The model number of the phone
 * @param string $manufacturer  The manufacturer of the phone
 *
 * @return string|bool Returns the phone on success. Returns FALSE if it does not exist.
 */
function find_phone_string(array $phones, $model, $manufacturer) {

    $input_model_pos        = strlen($model);
    $input_manufacturer_pos = strlen($manufacturer);

    $model        = strtolower($model);
    $manufacturer = strtolower($manufacturer);

    foreach($phones as $phone) {

        $phone_model        = substr($phone, 0, $input_model_pos);
        $phone_manufacturer = substr($phone, -$input_manufacturer_pos);

        if(strtolower($phone_model) == $model && strtolower($phone_manufacturer) == $manufacturer) {
            return $phone;
        }

    }

    return false;

}

The usage for both of them are the same regarding the argument list. Only the function name is different. Using the data you have provided the two function will return the following (displayed with var_dump()).

The regular expression version.

$phone = find_phone_regex($phones, '1845', 'nokia');

array (size=2)
  'phone' => string '1845-260-phone-nokia' (length=20)
  'number' => string '260' (length=3)

The string version.

$phone = find_phone_string($phones, '1845', 'nokia');

string '1845-260-phone-nokia' (length=20)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you AnotherGuy, I slightly change it to make it work for my case , thank you !
0
$array = array("1845-260-phone-nokia", "1133-0-phone-motorola", "1133-0-phone-samsung");
$search = 'nokia';
$results found = array();
foreach ($array as $key => $value){

    if (strpos($value, $search) !== false)
     $results_found[] = $value; 

}
echo 'following results found: '.implode('<br>', $results_found);

1 Comment

The is by far the fastest solution, but there might be more phones from Nokia in the array. This would only find the first Nokia phone.

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.