使用array_filter我可以使用搜索表单(文本输入)显示我的数组数据。但是我无法显示包含关键字的所有数组。
例如,我正在搜索关键字“产品1测试”
应显示名称为产品, 1 ,测试的所有产品以及包含产品的说明的产品, 1 ,测试
我的数组看起来像这样:
$p_arrays[] = array('name'=> 'Product 1','description' => 'Test Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'location' => 'A city', 'type' => 'Type 1', 'status' => 'new', 'tags'=>'', 'page_url'=>'p1.html', 'image'=>'products/assets/images/1/prod-image.jpg');
$p_arrays[] = array('name'=> 'Product 2','description' => 'Test Donec eleifend quam neque, ut mollis massa aliquet id.', 'location' => 'B city', 'type' => 'Type 1', 'status' => 'under', 'tags'=>'', 'page_url'=>'p2.html', 'image'=>'products/assets/images/2/prod-image.jpg');
$p_arrays[] = array('name'=> 'Product 3','description' => 'Nam non tristique mi.', 'location' => 'A city', 'type' => 'Type 3', 'status' => 'new', 'tags'=>'', 'page_url'=>'p3.html', 'image'=>'products/assets/images/3/prod-image.jpg');
$p_arrays[] = array('name'=> 'Product 4','description' => 'Vestibulum accumsan dolor id orci gravida viverra.', 'location' => 'C city', 'type' => 'Type 2', 'status' => 'new', 'tags'=>'', 'page_url'=>'p4.html', 'image'=>'products/assets/images/4/prod-image.jpg');
到目前为止我尝试了什么..
if (isset($_POST['keyword-input']) && $_POST['keyword-input']!=='') {
$keywords= ($_POST['keyword-input']);
$p_arrays = array_filter($p_arrays, function($p) use ($keywords) {
return (stripos(($p['description']), $keywords) !== false ) || (stripos(($p['name']), $keywords) !== false );
});
任何想法?
答案 0 :(得分:1)
以下是打破关键字并循环遍历数组的详细示例。
<?php
$p_arrays[] = array('name'=> 'Product 1','description' => 'Test Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'location' => 'A city', 'type' => 'Type 1', 'status' => 'new', 'tags'=>'', 'page_url'=>'p1.html', 'image'=>'products/assets/images/1/prod-image.jpg');
$p_arrays[] = array('name'=> 'Product 2','description' => 'Test Donec eleifend quam neque, ut mollis massa aliquet id.', 'location' => 'B city', 'type' => 'Type 1', 'status' => 'under', 'tags'=>'', 'page_url'=>'p2.html', 'image'=>'products/assets/images/2/prod-image.jpg');
$p_arrays[] = array('name'=> 'Product 3','description' => 'Nam non tristique mi.', 'location' => 'A city', 'type' => 'Type 3', 'status' => 'new', 'tags'=>'', 'page_url'=>'p3.html', 'image'=>'products/assets/images/3/prod-image.jpg');
$p_arrays[] = array('name'=> 'Product 4','description' => 'Vestibulum accumsan dolor id orci gravida viverra.', 'location' => 'C city', 'type' => 'Type 2', 'status' => 'new', 'tags'=>'', 'page_url'=>'p4.html', 'image'=>'products/assets/images/4/prod-image.jpg');
//if (isset($_POST['keyword-input']) && $_POST['keyword-input']!=='') {
//$keywords= ($_POST['keyword-input']);
$keywords = "Product 1 Test";
$keys = explode(" ", $keywords);
$p_arrays = array_filter($p_arrays, function($p) use ($keys) {
foreach($keys as $key){
echo (stripos(($p['description']), $key) !== false ) || (stripos(($p['name']), $key) !== false ) ? "Found $key in ". print_r($p,1) . "<br>\n" : "$key not found.<br>\n";
}
});
?>