PDO fetchAll array to one dimensional

前端 未结 3 1268
忘掉有多难
忘掉有多难 2020-11-29 03:57

this may be a simple question but am struggling to understand how to solve it. I have a form which allows the user to select either \"custom\" or \"all\" staff\" to assign t

相关标签:
3条回答
  • 2020-11-29 04:14

    You need to give fetchAll a fetch style

    $staff = $select->fetchAll(PDO::FETCH_NUM);
    

    From this link

    0 讨论(0)
  • 2020-11-29 04:22

    Take a look at example 2 in the manual. In your first query you can use:

    $staff = $select->fetchAll(PDO::FETCH_COLUMN, 0);
    

    And your second array will have the same form as the first array.

    0 讨论(0)
  • 2020-11-29 04:35

    If you print_r($staff[$i]) inside the for you would probably get

        Array
        (
            [staffID] => 1
            [0] => 1
        )
    

    which means you should use $staff[$i]['staffID'] instead.


    The other alternative, which should work with your current code, is to use PDOStatement::fetchColumn() instead of fetchAll().

    0 讨论(0)
提交回复
热议问题