0

我的 PHP 调用使用以下代码存储来自 mySQL 的表。

while (mysqli_stmt_fetch($stmtMyCountTotals)) {
    $stmtMyCountTotalsrow = array(
        'IndividualUnitsCounted' => $IndividualUnitsCounted, 
        'IndividualUnitsAdjusted' => $IndividualUnitsAdjusted
    );

    $stmtMyCountTotalsrows[] = $stmtMyCountTotalsrow;
}

$stmtMyCountTotals->close();

我似乎无法从中提取个人价值。例如,如果我想从第 1 列第 2 行中提取一个数字。我知道这是一个基本问题,但我似乎找不到答案。

4

4 回答 4

1

这是一个多维数组,因此您可以像这样访问它:

$row1col2 = $stmtMyCountTotalsrows[1][2]
$row2col1 = $stmtMyCountTotalsrows[2][1]

但由于它是关联的,你会想要:

$var = $stmtMyCountTotalsrows[1]['IndividualUnitsCounted'];

如果要按列访问它,则需要先将列名检索到数组中:

$cols = array_keys($stmtMyCountTotalsrows[0]);
$var = $stmtMyCountTotalsrows[1][$cols[1]]
//                                     ^ where this is the column number  
于 2013-07-19T16:14:34.263 回答
0

您的数组中没有列号,第二个维度是关联数组。所以应该是:

$stmtMyCountTotalsrows[$row_number]['IndividualUnitsCounted']
$stmtMyCountTotalsrows[$row_number]['IndividualUnitsAdjusted']
于 2013-07-19T16:18:01.003 回答
0

您在另一个数组中有一个数组。或多维数组。它看起来像这样:

Array(
  0 => Array(
            'IndividualUnitsCounted' => 'Something',
            'IndividualUnitsAdjusted' => 'Something Else'
            ),
  1 => Array(
            'IndividualUnitsCounted' => 'Yet another something',
            'IndividualUnitsAdjusted' => 'Final something'
            ),
  ...
  ...
)

如果调用此数组$stmtMyCountTotalsrows

$stmtMyCountTotalsrows[0]['IndividualUnitsCounted']返回“Something” $stmtMyCountTotalsrows[0]['IndividualUnitsCounted']返回“Something Else” $stmtMyCountTotalsrows[1]['IndividualUnitsAdjusted']返回“Yet another something” $stmtMyCountTotalsrows[1]['IndividualUnitsAdjusted']返回“Final something”

于 2013-07-19T16:21:11.397 回答
0

如果您事先不知道列的名称,则可以使用current()第一个:

echo current($stmtMyCountTotalsrows[1]);

或者,使用array_slice()

echo current(array_slice(stmtMyCountTotalsrows, 0, 1));
于 2013-07-19T16:21:46.027 回答