smrtr-datagrid

A 2D array wrapper with methods for operating on, importing & exporting tabular data

This project is maintained by smrtr

Searching

Previous: Iterating & Filtering

Inspired by jQuery, the search methods provided by Smrtr DataGrid comprise a prime featureset which take this class from just being a 2D array wrapper to being a manageable, query-able data store.

Methods

Smrtr DataGrid allows you to use selector strings to search the grid much like you would use CSS selectors to search the DOM tree in jQuery. Where the grid has labelled data, those labels can be used in a selector string to search for particular value(s) in the vector identified by that label.

searchRows( $expression )

Performs a search query on the grid’s rows based on the string $expression and returns a new \Smrtr\DataGrid object containing the matched rows.

Example:
<?php
$grid = new \Smrtr\DataGrid(array(
  array(1, 2, 3),
  array(2, 4, 6),
  array(4, 8, 12)
));
$grid->columnLabels(array('first', 'second', 'third'));
$result = $grid->searchRows('first=2');
print_r($result->getArray());
// [ [ 2, 4, 6 ] ]

searchColumns( $expression )

Performs a search query on the grid’s columns based on the string $expression and returns a new \Smrtr\DataGrid object containing the matched columns.

Example:
<?php
$grid = new \Smrtr\DataGrid(array(
  array(1, 2, 3),
  array(2, 4, 6),
  array(4, 8, 12)
));
$grid->rowLabels(array('row_A', 'row_B', 'row_C'));
$result = $grid->searchColumns('row_B=6');
print_r($result->getArray());
// [ [ 3, 6, 12 ] ]

Selectors

In the examples above we introduced the equality operator (field=value) which looks for values equal to the value provided. Our selectors are defined as follows:

Special Characters: + - , ( ) | " ' \ > = < ! ^ * $

These characters have special meaning, which will be explained in the following sections. Do not use these characters anywhere in your search expression unless you understand their meaning:

Fields

Examples:
<?php
$grid = new \Smrtr\DataGrid(array(
  array(1, 2, 3),
  array(2, 4, 6),
  array(4, 8, 12)
));
$grid->rowLabels(array('row_A', 'row_B', 'row_C'));
$grid->columnLabels(array('col_A', 'col_B', 'col_C'));

print_r($grid->searchRows('//=row_A')->getArray());
// [ [ 1, 2, 3 ] ]

print_r($grid->searchColumns('/=2')->getArray());
// [ [ 3, 6, 12 ] ]

print_r($grid->searchRows('col_A|/1=2')->getArray());
// [ [ 1, 2, 3 ], [ 2, 4, 6 ] ]
Escaping special characters in field names

Field names with special characters in them should be quoted.

If the field name also contains quote characters matching the quote character used to quote the field name, then those quote characters should be escaped with a backslash \.

If the value already contains backslashes, they should also be escaped with an extra backslash.

Examples:
<?php
$grid = new \Smrtr\DataGrid(array(
  array(1, 2, 8),
  array(2, 4, 16),
  array(4, 8, 32)
));
$grid->columnLabels(array('|', '||', '|||"'));

print_r($grid->searchRows('"|"|"||"=4')->getArray());
// [ [ 2, 4, 16 ], [ 4, 8, 32 ] ]

print_r($grid->searchRows('"||"|"|||\""=8')->getArray());
// [ [ 1, 2, 8 ], [ 4, 8, 32 ] ]

Operators

Examples:
<?php
$grid = new \Smrtr\DataGrid(array(
  array(1, 2, 3),
  array(2, 4, 6),
  array(4, 8, 12)
));
$grid->rowLabels(array('row_A', 'row_B', 'row_C'));
$grid->columnLabels(array('col_A', 'col_B', 'col_C'));

print_r($grid->searchColumns('row_C<10')->getArray());
// [ [ 1, 2, 4 ], [ 2, 4, 8 ] ]

print_r($grid->searchRows('col_B>=4')->getArray());
// [ [ 2, 4, 6 ], [ 4, 8, 12 ] ]

print_r($grid->searchRows('//^=row_')->getArray());
// [ [ 1, 2, 3 ], [ 2, 4, 6 ], [ 4, 8, 12 ] ]

print_r(
  $grid->searchRows('/0|/1|/2*=1')->getArray()
);
// [ [ 1, 2, 3 ], [ 4, 8, 12 ] ]

Values

Like fields, values can be grouped together with the | character to form a composite value (a value that matches whenever at least one of its sub-values matches).

Examples:
<?php
$grid = new \Smrtr\DataGrid(array(
  array("one", "two", "three"),
  array("two", "four", "six"),
  array("four", "eight", "twelve")
));
$grid->rowLabels(array('row_A', 'row_B', 'row_C'));
$grid->columnLabels(array('col_A', 'col_B', 'col_C'));

print_r($grid->searchRows('col_A=one|four')->getArray());
// [ [ "one", "two", "three" ], [ "four", "eight", "twelve" ] ]

print_r($grid->searchColumns('row_B*=w|x')->getArray());
// [ [ "one", "two", "four" ], [ "three", "six", "twelve" ] ]
Escaping special characters in values

Values with special characters in them should be quoted.

If the value also contains quote characters matching the quote character used to quote the value, then those quote characters should be escaped with a backslash \.

If the value already contains backslashes, they should also be escaped with an extra backslash.

Examples:
<?php
$grid = new \Smrtr\DataGrid(array(
  array('"Magic" Johnson', 100),
  array(' dragons', 64)
));
$grid->rowLabels(array('row1', 'row2'));
$grid->columnLabels(array('legend', 'rating'));

print_r($grid->searchRows('legend*="\"Magic\""')->toArray());
// [ [ '"magic" Johnson', 100 ] ]

Complex expressions

In this section we explain how to combine multiple selectors into a single search expression.

, OR

Use the , (comma) group operator to join two selectors A and B when you want to return all vectors that match A or B (or both).

Example:
<?php
$grid = new \Smrtr\DataGrid(array(
  array("Wayne", "Rooney", "striker", 27),
  array("Kieran", "Gibbs", "left back", 23),
  array("Gareth", "Barry", "midfielder", 32),
  array("Theo", "Walcott", "striker", 24)
));
$grid->columnLabels(array(
  "First Name", "Last Name", "Position", "Age"));
$grid->rowLabels(array("WR", "KG", "GB", "TW"));

print_r($grid->searchRows('Age<25 , Position=striker')->getRowLabels());
// [ "KG", "TW", "WR" ]

+ AND

Use the + (plus) group operator to join two selectors A and B when you want to return all vectors that match both A and B.

Example:
<?php
$grid = new \Smrtr\DataGrid(array(
  array("Wayne", "Rooney", "striker", 27),
  array("Kieran", "Gibbs", "left back", 23),
  array("Gareth", "Barry", "midfielder", 32),
  array("Theo", "Walcott", "striker", 24)
));
$grid->columnLabels(array(
  "First Name", "Last Name", "Position", "Age"));
$grid->rowLabels(array("WR", "KG", "GB", "TW"));

print_r($grid->searchRows('Age<25 + Position=striker')->getRowLabels());
// [ "TW" ]

- NOT

Use the - (minus) group operator to join two selectors A and B when you want to return all vectors that match A but not B.

Example:
<?php
$grid = new \Smrtr\DataGrid(array(
  array("Wayne", "Rooney", "striker", 27),
  array("Kieran", "Gibbs", "left back", 23),
  array("Gareth", "Barry", "midfielder", 32),
  array("Theo", "Walcott", "striker", 24)
));
$grid->columnLabels(array(
  "First Name", "Last Name", "Position", "Age"));
$grid->rowLabels(array("WR", "KG", "GB", "TW"));

print_r($grid->searchRows('Age<25 - Position=striker')->getRowLabels());
// [ "KG" ]

Nested Expressions

Nested expressions are also supported using mathematical bracket notation with ( and ).

Examples:
<?php
$grid = new \Smrtr\DataGrid(array(
  array("Wayne", "Rooney", "striker", 27),
  array("Kieran", "Gibbs", "left back", 23),
  array("Gareth", "Barry", "midfielder", 32),
  array("Theo", "Walcott", "striker", 24)
));
$grid->columnLabels(array(
  "First Name", "Last Name", "Position", "Age"));
$grid->rowLabels(array("WR", "KG", "GB", "TW"));

$s = 'Age<30 + (Position="left back" , Position=midfielder)';
print_r($grid->searchRows($s)->getRowLabels());
// [ "KG" ]

$s = '(Age<30 + Position="left back") , Position=midfielder';
print_r($grid->searchRows($s)->getRowLabels());
// [ "KG", "GB" ]