2
Array
(
    [0] => LAMPION
    [1] => BANBU
    [2] => DT-T300-FNS
    [3] => T65
    [4] => DT-299-FNS
    [5] => T30
)

I have an array looking like this. The problem is the data stored in the array is not consistent so i have to search the array for this pattern "xx-xxx-xxx" and store it in a variable. is there any way i can do that? really need hlp

1 Answer 1

2

Yes.

$matches = preg_grep('/^.{2}-.{3}-.{3}\z/', $array);

If you want the first, just add [0] (you'll need a temporary variable first for < PHP 5.4).

Sign up to request clarification or add additional context in comments.

5 Comments

or use current() to get the first one :)
@Jack Oh yeah, it's been a while :)
what if ill remove all value that matches to $matches from the mother array?
@EllandJunsayPansensoy you want to remove the found entries from the original array? array_diff() could do that for you I think.
@EllandJunsayPansensoy If you want to remove all members which don't match, just assign this return value to your original array. If you want the inverse, do the same but use the PREG_GREP_INVERT flag.

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.