0

I have a search text field, and an array I want to output below.

This is my array:

array=["abc","abcde","ab","abcdef"];

When I enter "ab" in the text field, then the list should appear. "ab" should come first.

ab,
abc,
abcde,
abcdef,

If I type "abc" then list should show:

abc,
abcde,
abcdef,
2
  • This is search by keyword from Array response please help in this. Commented Mar 1, 2019 at 9:15
  • 3
    What have you tried so far? Commented Mar 1, 2019 at 13:33

2 Answers 2

0

This should solve your little issue

<?php
$input = preg_quote('cde', '~'); // don't forget to quote input string!
$array=["abc","abcde","ab","abcdef"];
$result = preg_grep('~' . $input . '~', $array);
foreach ($result as $val) {
    echo "$val\n";
}
?>

Check this link for more

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

1 Comment

Thank you for your replay:- If I search for "cde" I have to get Below result :- abcde abcdef But it gives me the same
0
<?php
$array = ["abc","abcdeab","ab","abcdef"];
$arr = preg_grep('/cde/', $array);
sort($arr);
var_dump($arr);
?>

check this out

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.