0

I have following example array,

Array
(
    [0] => TEST1
    [1] => TEST2
    [2] => TEST3
    [3] => TEST4
    [4] => TEST
    ....
)

Now in Database values stored as

/TEST/IMAGE
/TEST2/VIDEO
/TEST3/MIME
...

Now I want to match the substring found in array when to fetch the data from database:

So, I have applied following query:

SELECT * FROM media_library WHERE 1 AND sDirectory REGEXP '(/TEST1.*)|(/TEST2.*)|(/TEST3.*)|(/TEST4.*)|(/TEST.*)'

But for the syntax '(/TEST1.*)|

I have used for loop

$data = '';$k=0;
foreach($folders as $key => $val){
    $data .= ($k>0) ? "|" : "";
    $data .= "(/".$val.".*)";
    $k++;
}

So, Is there any way to directly use an array which creates format very easily rather than for loop?

Thanks

1 Answer 1

1

You can use http://php.net/manual/en/function.array-map.php and after that use implode().

Something like that:

$regex = implode('|', array_map(function ($value) {
    return "(/{$value}.*)";
}, $yourarray));
Sign up to request clarification or add additional context in comments.

Comments

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.