'How to use a value from $_POST as the parameter of an in_array function?

I have built an HTML form to search through a .csv and return all entries that match the specific input string.

My aim is to be able to search through a .csv and return all the 3d array entries with matching values in specific columns.

I have currently got it working by hardcoding in the search term, but my aim is to use the $_POST value from the HTML text input, so the search is actually dynamic.

Tools is the currently hardcoded search term, I have attempted to replace this with $_POST['components'] but this currently returns an Undefined index error: components.

The output is currently showing all array entries with Tools as the component, regardless of what is entered in the input box.

    $csv = array_map('str_getcsv', file('C:\Work\issues_1y_filled_in.csv'));

$group_ouput = array_filter($csv, function($key)
{   
    return in_array('Tools', $key);
});


if (isset($_POST['components'])) 
{
    $group = $_POST ['components'];
    print_r(pre_r($_POST));
    echo '<pre>'; print_r($group_ouput); echo '</pre>';
}


function pre_r($array)
{
    echo '<pre>';
    print_r($array);
    echo '</pre>';
}


    ?>
<form method="POST" enctype="multipart/form-data">
    <br>
    <br>

    <div>
        <label for="months">Months</label>
        <input type="number" name="months" id="months" placeholder="Enter # of months" min="3" max="24" step="3">
    </div>

    <div>
        <label for="components">Component</label>
        <input type="text" id="components" name="components">
    </div>

    <input type="submit" name="submit" value="Submit">

    <br>

    <button type="reset">Reset fields</button>

</form>

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source