0

I have myself in a unique situation here and I am not sure if this is the correct way to go about it; I am open to suggestions.

I have a function in which it grabs all of the table names in a database and stores them into an array. Next newly parsed items ($id) are passed against this table name array and any matches are unset from this array. This leaves me with the leftovers which are items that have been discontinued.

Code below:

function itemDiscontinued($dbh, $id, $detail) {
    try {   
        $tableList = array();
        $result = $dbh->query("SHOW TABLES");
        while ($row = $result->fetch()) {
            $tableList[] = $row[0];
        }
        $key = array_search($id, $tableList);
        unset($tableList[$key]);
        print_r($tableList);
       }
    catch (PDOException $e) {
        echo $e->getMessage();
    }
}

The problem is that the array $tablelist keeps recreating itself due to the function being in a foreach loop (Parsing process). I only require one instance of it to work with once it is created. I apologise before hand if the problem is a bit hard to understand.

Hemesh
  • 329
  • 2
  • 3
  • 13

2 Answers2

1

Yea, it's really hard to understand. Maybe you'll try this:

function itemDiscontinued($dbh, $id, $detail) {
    static $tables = array();
    if (!$tables) {
        $tables = getTableList($dbh);
    }
    $key = array_search($id, $tables);
    unset($tables[$key]);
    print_r($tables);
}

function getTableList($dbh) {
    try {
        $tableList = array();
        $result = $dbh->query("SHOW TABLES");
        while ($row = $result->fetch()) {
            $tableList[] = $row[0];
        }
        return $tableList;
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}
picios
  • 250
  • 1
  • 6
  • Here is what i'm getting so far even with that change you suggested. `338142: Table ALREADY exists-->No price change found2Array ( [0] => 113340 [1] => 116516 [3] => 20731 [4] => 25500 [5] => 322320 [6] => 322459 [7] => 323003 [8] => 325958 [9] => 325961 [10] => 325963 [11] => 326002 [12] => 326035)` `139431: Table ALREADY exists-->No price change found3Array ( [0] => 113340 [1] => 116516 [2] => 139431 => 20731 [4] => 25500 [5] => 322320 [6] => 322459 [7] => 323003 [8] => 325958 [9] => 325961 [10] => 325963 [11] => 326002 [12] => 326035)` Please note the number before the array is the $key :) – Hemesh Apr 25 '13 at 22:19
  • Thank you picios! That worked exactly the way I wanted it to do. It depleted that array to the left over items. :) – Hemesh Apr 25 '13 at 23:32
1

how about an array_push with an extra parameter

function itemDiscontinued($dbh, $id, $detail, $outputArray) {
    try {   
        $result = $dbh->query("SHOW TABLES");
        while ($row = $result->fetch()) {
            array_push($outputArray, $row[0]);
        }
        $key = array_search($id, $outputArray);
        unset($outputArray[$key]);
        return $outputArray; // use this for subsequent run on the foreach statment
       }
    catch (PDOException $e) {
        echo $e->getMessage();
    }
}
Dan
  • 3,755
  • 4
  • 27
  • 38