Search code examples
phparraysarray-filter

Get the array value that contains keywords


Using array_filter I am able to show my arrays data using a search form (text input). But I am not able to show all the array containing the keywords.

For example I am searching keyword 'Product 1 test'

It should show all product with name Product, 1, test and product with description that contains Product, 1, test

My array looks like this:

$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');

What I have tried so far..

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 );

});

Any Ideas?


Solution

  • Here's a verbose example of breaking keywords up and looping through the array.

    <?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";
        }
    
    });
    
    
    ?>