3

I'm looking the way to find one string in array of strings:

$array = ['string1'=>'custom', 'other string'=>'another', 'another'=>'again', 'custom'=>'pid|100', 'once again'=>'pid|'];

I need to find this element: pid|100;

String should containt: pid|any positive integer number

foreach($array as $value) { 
   // regular expression here
}

How can I found this value? Thanks!

0

2 Answers 2

12

You want to use preg_grep for this :)

print_r(preg_grep('/^pid\|[0-9]+$/',$array));
Sign up to request clarification or add additional context in comments.

2 Comments

Please take a look to my updated question. I have a bit another array.
It should act the same on associative arrays. If you only want the values you can use array_values on the return of preg_grep to make the results indexed.
1

in_array(preg_match('/^pid\|[0-9]+$/',$your_array),$your_array);

OR

in_array(preg_grep('/^pid\|[0-9]+$/',$your_array),$your_array);

if you need to check this particular element i.e. pid|100 then just use in_array('pid|100',$your_array) there is no need to check using regular expression

hope this will help you in solve your problem

2 Comments

Thank you. Take a look to my updated question please. My input data has another structure.
if you need to check this particular element i.e. pid|100 then just use in_array('pid|100',$your_array) there is no need to check using regular expression

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.