3

The Problem

Ignoring specific values, when arrays are pulled, using medoo, and merged.

The Details

Say I have two arrays, created from two similar tables. They have a 'user'-row and an 'id'-row. Example below:

id        name   
-------   ---------
  1         John Doe     
  2         John Snow    
  3         Jane     
  4         Jane       

What is the best way to ignore all entries that have 'John ...' in the name row? My array_merge is currently like this:

  public function getUsersByName($name)
    {
        $res = array_merge(
            $this->_db->select('users1', ['id', 'name'],
            $this->_db->select('users2', ['id', 'name']
        );
        return $res;
    }

This returns a nice array, with all values in the tables. What is the best practice to ignore those values, and skip to the next legitimate value?

eengstroem
  • 143
  • 1
  • 9

1 Answers1

2

To run queries with WHERE .. NOT LIKE .. in medoo, you can pass a third parameter to select() that contains an array of the where clauses. The key is the column with the operator, and the value is - well, the value.

For example, to select from users1 where no names begin with John, you'll need

$this->_db->select('users1', ['id', 'name'], ['name[!~]' => 'John']);

The [!~] signalizes NOT LIKE.

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • Thank you Qirel, I can confirm that this has the desired effect of excluding certain calues. – eengstroem Nov 03 '20 at 09:24
  • ['name[!~]' => 'John'] is equivalent to name NOT LIKE "%John%". Use ['name[!~]' => 'John%'] if you only want to exclude name starting with John, also is faster (equivalent to name NOT LIKE "John%"). – Velome Nov 19 '20 at 06:31