0

i have an array:

$mainArr = ["SRI", "AIS", "GOW","SRI#AIS","SRI#GOW", "SRI#GOW#AIS"];

$strArr = ["SRI"];

i want to search the main Array with the given string Array element so that if the string is matched it should get the corresponding key,value pair.

expected o/p would be:

Array[
    0->SRI
    3->SRI#AIS
    4->SRI#GOW
    5->SRI#AIS#GOW
]

Any ideas ?

Thanks, Srinivas

3 Answers 3

1
$mainArr = array("SRI", "AIS", "GOW","SRI#AIS","SRI#GOW", "SRI#GOW#AIS");

$strArr = array("SRI");
foreach ($mainArr as $key => $value)
{
    foreach ($strArr as $str)
    {
        if (strpos($value,$str) !== false) $rez[$key] = $value;
    }
}

var_dump($rez);

output:

    array(4) { 
[0]=> string(3) "SRI" 
[3]=> string(7) "SRI#AIS" 
[4]=> string(7) "SRI#GOW" 
[5]=> string(11) "SRI#GOW#AIS" 
} 
Sign up to request clarification or add additional context in comments.

7 Comments

i dont think foreach will workout here, bcoz my original array has 4000 elements and string array contains 10 elements.is there any option like in_array ? or array_search ?
if u have so many elements u have to create another array where u'll store all matches. in_array and array_search can't be used straight since u don't want to find strings exactly the same as ur search pattern
i think using array filter with the logic u have mentioned with strpos will workout here, instead of using two foreach loops.
i liked your answer but not able to increase the votes since iam new member overh here. Thanks for the help :)
emm... i can't figure out how u can pass $strArr to the filter function
|
0

I thing i ll help u,,

$test=array();
        $mainArr = array("SRI", "AIS", "GOW","SRI#AIS","SRI#GOW", "SRI#GOW#AIS");
        $strArr = array("SRI");
        foreach ($mainArr as $key => $value)
        {
            $temp   = explode('#',$value);
            //$temp = $temp[0];
            if(in_array($temp[0],$strArr))
            $test[$key]=$value; 
        }
        echo "<pre><span style='color:black; font-size:19;'>";print_r($test);echo "</span></pre>";

2 Comments

@k102:2nd Forecho i thing it wont nessary for his coding
your solution has several limitations: what if i have element like this: "SRI||AIS" or what if like this: "GOW#SRI". this is why i used two foreach
0

Use array_filter:

function filter($element)
{
    return strpos($element, 'SRI') !== false;
}

$mainArr = array("SRI", "AIS", "GOW","SRI#AIS","SRI#GOW", "SRI#GOW#AIS");
$filteredArr = array_filter($mainArr, 'filter');

1 Comment

No problem :) Note that in the function you're testing with the strict inequality operator, as strpos() might return false or 0, depending on whether the substring you're looking for is not present at all, or is merely at the beginning of the string.

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.