简体   繁体   English

来自SQL Query的foreach循环的数组结果

[英]Array outcome from foreach loop from SQL Query

Looking for the code below to create an array of 1's and 0's from this query. 寻找下面的代码,从这个查询创建一个1和0的数组。 At the moment, the result from $rows is: 目前,$ rows的结果是:

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();
    }
}

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的值, $pdo->fetch() ,要进行数字索引,你可以将参数中的fetch样式设置为PDO::FETCH_NUM而不是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() : 但是,如果您正在寻找所有记录的“完整数组”,则可以切换到fetchAll()而不是像fetch()那样拉入单个结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM