SQL-based manipulations for PHP arrays