HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Optimize PHP foreach loops

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
foreachphploopsoptimize

Problem

By any means, if any can tell me if this code can be more optimized than it already is, please say so. It's a select statement to my PDO class. When I glare over my code, I'm certain there is a lot of easy improvements.

``
public function select($table, $where = array(), $Innerjoin = array(), $group = array(), $selects = array()) {

$vowels = array("a", "e", "i", "o", "u", "y", "A", "E", "I", "O", "U","Y");

$select = "";
if(empty($selects)){
$select .= 'SELECT *';
} else {
foreach($selects as $key => $value){
$select == "" ? $select = "SELECT " : $select .= " , ";

$select .= str_replace($vowels, "" , $key) . "." . $value;
}
}

$sql = $select . " FROM
" . $table . " AS " . str_replace($vowels, "" , $table);

if(!empty($Innerjoin)){
$join = "";
foreach ($Innerjoin as $joinKey => $joinValue) {
foreach ($joinValue as $key => $value) {
$join .= " INNER JOIN
" . $joinKey . "` AS " . str_replace($vowels, "" , $joinKey) . " ON ";
if(is_array($value)){
foreach ($value as $ids => $id) {
$join .= str_replace($vowels, "" , $ids) . "." . $key . " = " . str_replace($vowels, "" , $joinKey) . "." . $id;
}
} else {
$join .= str_replace($vowels, "" , $table) . "." . $key . " = " . str_replace($vowels, "" , $joinKey) . "." . $key;
}
}
}
$sql .= $join;
}

if(!empty($where)){
$qWhere = "";
foreach($where as $key => $value){
foreach($value as $operator => $attribute){
$qWhere == "" ? $qWhere = " WHERE " : $qWhere .= " AND ";

$qWhere .= "" . str_replace($vowels, "" , $table) . "." . $key . " " . $operator .

Solution

As you use $vowels in several different methods, let's start by making that a static member of your class:

class PDO
{
    private static $vowels = array("a", "e", "i", "o", "u", "y", "A", "E", "I", "O", "U", "Y");
}


You also use this twice in a foreach loop:

str_replace(self::$vowels, "", $key) . "." . $value;


And manually implode some queries with it. To make it generic, have a method which removes vowels in this way, such as:

private static function removeVowels(array $array)
{
    $removed = array();
    foreach($array as $key => $value)
        $removed[] = str_replace(self::$vowels, '', $key) . '.' . $value;
    return $removed;
}


With this, you can remove the first if/else and foreach loop entirely, by removing the ternary and using implode(), like so:

$select = 'SELECT ' . (empty($selects) ? '*' : implode(', ', self::removeVowels($selects)));


or even removing the select variable altogether and just collapsing it into your $sql:

$sql = 'SELECT ' . (empty($selects) ? '*' : implode(', ', self::removeVowels($selects))) .
       ' FROM `' . $table . '` AS ' . str_replace(self::$vowels, '', $table);


And using the same style, you can reduce your Group By block to:

if(!empty($group))
{
    $sql .= " GROUP BY (" . implode(', ', self::removeVowels($group)) . ')';
}


You also use this twice:

str_replace(self::$vowels, "", $table) . "." . $key . " " . $operator . " '" . $attribute . "'"


Which can be separated out in the same way:

private static function removeVowelsWhere(array $array, $table)
{
    $removed = array();
    foreach($array as $key => $value)
    {
        foreach($value as $operator => $attribute)
        {
            $removed[] = str_replace(self::$vowels, "", $table) . "." . $key . " " . $operator . " '" . $attribute . "'";
        }
    }
    return $removed;
}


To simplify your where blocks to:

if(!empty($where))
{
    $sql .= " WHERE " . implode(' AND ', self::removeVowelsWhere($where, $table));
}

Code Snippets

class PDO
{
    private static $vowels = array("a", "e", "i", "o", "u", "y", "A", "E", "I", "O", "U", "Y");
}
str_replace(self::$vowels, "", $key) . "." . $value;
private static function removeVowels(array $array)
{
    $removed = array();
    foreach($array as $key => $value)
        $removed[] = str_replace(self::$vowels, '', $key) . '.' . $value;
    return $removed;
}
$select = 'SELECT ' . (empty($selects) ? '*' : implode(', ', self::removeVowels($selects)));
$sql = 'SELECT ' . (empty($selects) ? '*' : implode(', ', self::removeVowels($selects))) .
       ' FROM `' . $table . '` AS ' . str_replace(self::$vowels, '', $table);

Context

StackExchange Code Review Q#51746, answer score: 5

Revisions (0)

No revisions yet.