0

Looking for the code below to create an array of 1's and 0's from this query. At the moment, the result from $rows is:

Array ( [discontinued] => 1, ) Array ( [discontinued] => 1 ) Array ( [discontinued] => 0 ) Array ( [discontinued] => 0 ) Array ( [discontinued] => 1 ).... 

where I would rather have something like:

Array [1] => 1 [2] => 1 [3] => 0 [4] => 0 [5] => 1....

Anyone could help me out on this, it would be greatly appreciated. Please don't comment about vulnerabilities in the code; I know it's already there. I am just a beginner...I just want the core guts of it to work.

Cheers! :)

function checkDiscontinued($dbh, $idDiscontinuedArray) {
try {
    foreach ($idDiscontinuedArray as $id) {

        $stmt = $dbh->query("SELECT discontinued FROM `$id` ORDER BY `date` DESC LIMIT 1");
        $rows = $stmt->fetch(PDO::FETCH_ASSOC);

        print_r($rows);

        }
        if ($rows['discontinued'] == TRUE) { 
            //echo $id . "Action if true";
        } else {
            changeDiscontinued($dbh, $id, $idDiscontinuedArray);
            //echo $id . "Items already discontinued!";
            }       
    }
    catch (PDOException $e) {
    echo $e->getMessage();
    }
}

1 Answer 1

2

If you're looking to have the values in $rows, the result of $pdo->fetch(), to be numerically indexed, you can set the fetch style in the parameters to PDO::FETCH_NUM instead of PDO::FETCH_ASSOC:

$rows = $stmt->fetch(PDO::FETCH_NUM);

However, if you're looking for a "full array" of all records, you can switch to fetchAll() instead of pulling in a single result as with fetch():

$rows = $stmt->fetchAll(PDO::FETCH_NUM);
Sign up to request clarification or add additional context in comments.

1 Comment

Is there anyway I could modify my code to get something like Array [1] => 1 [2] => 1 [3] => 0 [4] => 0 [5] => 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.