是否可以像在MySQL中一样在PHP数组中进行搜索。
例如:我有array
array(
'mark@test.com'=> `Mark Mian`,
'jhon@test.com'=> `John jack`,
'logon@test.com'=> `Bob Logon`,
'Stela@test.com'=> `Stela Josh`,
'json@test.com'=> `Json Josh`
'bobby@test.com'=> `Bob Mark`
)
我会做这种搜索,
例如:如果我搜索Mark
,则应该返回此
'mark@test.com'=> `Mark Mian
如果我搜索Bob
它应该返回
'bobby@test.com'=>
Bob Mark
'logon@test.com'=>
Bob Logon
,
如果我仅搜索a
,则应该返回包含a
的元素,例如:
'mark@test.com'=>
Mark Mian
,'jhon@test.com'=>
John jack
,'Stela@test.com'=>
Stela Josh
,'bobby@test.com'=>
Bob Mark
注意:搜索应按键或值
答案 0 :(得分:7)
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
dataGridView1.Visible = true;
btn_Add.Visible = true;
button1.Visible = false;
label1.Visible = true;
label2.Visible = true;
textBox2.Visible = true;
booklist = new List<Book>();
booklist.Add(new Book("001", "Lord Of the Rings", "J. R. R. Tolkien", "5", "0 Books Avaible"));
booklist.Add(new Book("002", "The Hobbit", "J. R. R. Tolkien", "5", "2 Books Avaible"));
booklist.Add(new Book("003", "The Lion, the Witch and the Wardrobe", "C. S. Lewis", "5", "1 Books Avaible"));
booklist.Add(new Book("004", "The Alchemist", "Paulo Coelho", "5", "3 Books Avaible"));
booklist.Add(new Book("005", "Think and Grow Rich", "Napoleon Hill", "5", "4 Books Avaible"));
booklist.Add(new Book("006", "The Da Vinci Code", "Dan Brown", "5", "3 Books Avaible"));
booklist.Add(new Book("007", "And Then There Were None", "Agatha Christie", "5", "5 Books Avaible"));
booklist.Add(new Book("008", "She: A History of Adventure", "H. Rider Haggard", "5", "0 Books Avaible"));
booklist.Add(new Book("009", "Dream of the Red Chamber", "Cao Xueqin", "5", "5 Books Avaible"));
booklist.Add(new Book("010", "The Catcher in the Rye", "J. D. Salinger", "5", "5 Books Avaible"));
booklist.Add(new Book("011", "The Little Prince", "Antoine de Saint-Exupéry", "5", "5 Books Avaible"));
booklist.Add(new Book("012", "Lolita", "Vladimir Nabokov", "5", "3 Books Avaible"));
booklist.Add(new Book("013", "The Name of the Rose", "Umberto Eco", "5", "1 Books Abaible"));
booklist.Add(new Book("014", "Black Beauty", "Anna Sewell", "5", "2 Books Avaible"));
booklist.Add(new Book("015", "Charlotte's Web", "E.B. White", "5", "0 Books Avaible"));
booklist.Add(new Book("016", "Harry Potter and the Goblet of Fire", "J. K. Rowling", "5", "2 Books Avaible"));
booklist.Add(new Book("017", "Harry Potter and the Prisoner of Azkaban", "J. K. Rowling", "5", "1 Books Avaible"));
booklist.Add(new Book("018", "Harry Potter and the Chamber of Secrets", "J. K. Rowling", "5", "4 Books Avaible"));
booklist.Add(new Book("019", "Harry Potter and the Half-Blood Prince", "J. K. Rowling", "5", "3 Books Avaible"));
booklist.Add(new Book("020", "Harry Potter and the Philosopher's Stone", "J. K. Rowling", "5", "3 Books Avaible"));
booklist.Add(new Book("021", "Harry Potter and the Order of the Phoenix", "J. K. Rowling", "5", "4 Books Avaible"));
booklist.Add(new Book("022", "Harry Potter and the Deathly Hallows", "J. K. Rowling", "5", "3 Books Avaible"));
booklist.Add(new Book("023", "", "", "", ""));
booklist.Add(new Book("024", "", "", "", ""));
booklist.Add(new Book("025", "", "", "", ""));
booklist.Add(new Book("026", "", "", "", ""));
dataGridView1.Columns.Clear();
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = booklist;
}
private void btn_Add_Click(object sender, EventArgs e)
{
btn_Add.Visible = false;
button3.Visible = true;
dataGridView1.Visible = true;
dataGridView1.AllowUserToAddRows = true;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
var textbox = (TextBox)sender;
if (string.IsNullOrEmpty(textbox.Text))
{
dataGridView1.DataSource = booklist;
return;
}
var search = booklist.Where(b => b.Name.Contains(textbox.Text)).ToList();
dataGridView1.DataSource = search;
Console.WriteLine(e.ToString());
}
<强> Fiddle 强>
如果您想要搜索键和值两者,如果您只想搜索值,请删除键部分。
答案 1 :(得分:6)
这是一个preg_grep
解决方案,应该更像MySQL中的WHERE REGEXP 'PATTERN'
。我修改了以在数组键中搜索模式,并为其添加了Daniel Klein's preg_grep_keys
function,其中应该与具有非数字键的数组一起使用。如果密钥是数字的,只需使用array_merge
preg_grep('~Mark~i', $arr);
来查找包含mark
或Mark
等所有数组元素。)
preg_grep
solution
将一个或多个数组的元素合并在一起,以便将一个值的值附加到前一个数组的末尾。它返回结果数组。 如果输入数组具有相同的字符串键,则该键的后一个值将覆盖前一个键。但是,如果数组包含数字键,则后面的值不会覆盖原始值,但是会被追加。
function preg_grep_keys_values($pattern, $input, $flags = 0) {
return array_merge(
array_intersect_key($input, array_flip(preg_grep($pattern, array_keys($input), $flags))),
preg_grep($pattern, $input, $flags)
);
}
$a = array(
'mark@test.by.com'=> "Mark Mian lv",
'jhon@test.lv.com'=> "John jack lv",
'logon@test.en.com'=> "Bob Logon",
'Stela@test.es.com'=> "Stela Josh",
'json@test.es.com'=> "Json Josh",
'bobby@test.lv.com'=> "Bob Mark"
);
$r = preg_grep_keys_values('~lv~i', $a);
print_r($r);
请参阅array_merge
上面的代码首先在键中搜索lv
(不区分大小写),然后在值中搜索,然后将结果合并到1个数组中。因此,结果是:
[jhon@test.lv.com] => John jack lv
[bobby@test.lv.com] => Bob Mark
[mark@test.by.com] => Mark Mian lv
答案 2 :(得分:4)
一种简单的方法是使用array_filter
如果你想要正则表达式,那就行了
$regex = '~test~';
$result = array_filter($data, function($item) use ($regex) {
return preg_match($regex, $item);
});
或者只是一个简单的包含搜索
$search = 'test';
$result = array_filter($data, function($item) use ($search) {
return stristr($value, $search);
});
如果必须同时搜索 - 键和值,可以将参数ARRAY_FILTER_USE_BOTH
附加到array_filter。
$search = 'test';
$result = array_filter($data, function($item, $key) use ($search) {
return stristr($value, $search) || stristr($key, $search);
}, ARRAY_FILTER_USE_BOTH);
最后,你可以将array_filter与preg_grep结合起来,一次搜索两个。
$search = '~bob~i';
$result = array_filter($data, function() use ($search) {
return count(preg_grep($search, func_get_args()));
}, ARRAY_FILTER_USE_BOTH);
答案 3 :(得分:2)
您想要过滤数组,请使用专为此设计的[ContentType(DisplayName = "ImageFile", GUID = "0b6fe917-9aa6-4e7b-9fae-1ff21a39a614", Description = "")]
[MediaDescriptor(ExtensionString = "jpg,jpeg,jpe,ico,gif,bmp,png")]
public class ImageFile : MediaData
{
public virtual string Copyright { get; set; }
}
。
如果只搜索文字字符串,则不需要使用正则表达式:
array_filter
如果您希望能够使用正则表达式进行过滤:
$needle = 'bob';
$result = array_filter($data, function ($k, $v) use ($needle) {
return stripos($k, $needle) !== false || stripos($v, $needle) !== false;
}, ARRAY_FILTER_USE_BOTH);
答案 4 :(得分:0)
$search = "Mark"
$array = array(
'mark@test.com'=> `Mark Mian`,
'jhon@test.com'=> `John jack`,
'logon@test.com'=> `Bob Logon`,
'Stela@test.com'=> `Stela Josh`,
'json@test.com'=> `Json Josh`
'bobby@test.com'=> `Bob Mark`
)
foreach ($array as $key => $value) {
if (stristr($value, $search) == '') {
//not found
}else{
//found
}
这是搜索任何不区分大小写且快速的子字符串的最佳方法
就像在mysql中一样
例如:
从名称=“%Mark%”的表中选择*