How to create a table with a filter? I am using drupal 7

The following code turns an array into a pagered table.

$rows = $data[data];
$per_page = 30;
// Initialize the pager
$current_page = pager_default_initialize(count($rows), $per_page);
// Split your list into page sized chunks
$chunks = array_chunk($rows, $per_page, TRUE);
// Show the appropriate items from the list
$output = theme('table', array('header' =&gt; $header, 'rows' =&gt; $chunks[$current_page]));
// Show the pager
$output .= theme('pager', array('quantity',count($rows)));
echo $output;


Is it possible to add filters to the table?

Thanks

Updated 21/04/2016 as Per Neograph734s answer

I have built a module based around the code Neograph734 gave as an example.

My array is built like

Array
(
    [0] =&gt; Array
        (
            [EventTime] =&gt; 23/04/2016 15:00
            [EventType] =&gt; Foo
            [EventName] =&gt; Bar 
            [EventRating] =&gt; 99.12
        )

    [1] =&gt; Array
        (
            [EventTime] =&gt; 23/04/2016 18:00
            [EventType] =&gt; FooFoo
            [EventName] =&gt; BarBar 
            [EventRating] =&gt; 94.84
        )


The only ammendment I have made is in field to seach as below

foreach ($rows as $key =&gt; $row) {
  if (preg_match("/" . $form_state['storage']['title'] . "/", $row['EventName'])) {
    $temp_rows[$key] = $row;
  }
}


The table initially loads, I have a text area and a submit button. When I enter a search term the page reloads but the table does not filter, it still shows all results.

The html out put is

&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;23/04/2016 15:00&lt;/td&gt;
&lt;td&gt;Foo&lt;/td&gt;
&lt;td&gt;Bar&lt;/td&gt;
&lt;td&gt;99.12&lt;/td&gt;
&lt;/tr&gt;


What more do I have to change to get the text field to search the results?

Thanks
It can be done by wrapping your table in a form, and then use the form API to render your table and re-render it on submit. You can have a look at this comment: https://www.drupal.org/node/1770512#comment-6428358

In short the render funtion:

function MY_MODULE_build_form($form, &amp;$form_state) {
  // Filter field.
  $form['title'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Title to look for'),
    '#size' =&gt; 15,
    '#default_value' =&gt; isset($form_state['storage']['title']) ? $form_state['storage']['title'] : '',
  );

  // Submit button.
  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t("Find"),  
  );

  // Modify table array here. 
  // If your API supports filters, you can use that too. 
  // My regex is bad, so you might need to tweak this.
  if (isset($form_state['storage']['title'])) {
    $temp_rows = array();

    foreach ($rows as $key =&gt; $row) {
      if (preg_match("/" . $form_state['storage']['title'] . "/", $row['field_to_search'])) {
        $temp_rows[$key] = $row;
      }
    }

    $rows = $temp_rows;
  }

  // Render the table array.
  $form['table'] = array(
    '#theme' =&gt; 'table',
    '#header' =&gt; $header,
    '#rows' =&gt; $rows,
    '#empty' =&gt; t('Empty Rows')
  );      

  return $form;
}


And the Submit function 

function MY_MODULE_build_form_submit($form, &amp;$form_state) {
  // Set the provided filter value in the storage.
  $form_state['storage']['title'] = $form_state['values']['title'];
  // Show the form again.
  $form_state['rebuild'] = TRUE;  
}


You might want to fancy it up with some fieldsets, but those are left out to keep the answer understandable.