来自多维数组的回声值

时间:2013-07-19 16:12:33

标签: php mysql arrays

我的PHP调用是使用以下代码存储mySQL中的表。

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

    $stmtMyCountTotalsrows[] = $stmtMyCountTotalsrow;
}

$stmtMyCountTotals->close();

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

4 个答案:

答案 0 :(得分: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  

答案 1 :(得分:0)

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

$stmtMyCountTotalsrows[$row_number]['IndividualUnitsCounted']
$stmtMyCountTotalsrows[$row_number]['IndividualUnitsAdjusted']

答案 2 :(得分: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']返回“其他内容” $stmtMyCountTotalsrows[1]['IndividualUnitsAdjusted']返回“还有一件事” $stmtMyCountTotalsrows[1]['IndividualUnitsAdjusted']返回“最终内容”

答案 3 :(得分:0)

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

echo current($stmtMyCountTotalsrows[1]);

或者,使用array_slice()

echo current(array_slice(stmtMyCountTotalsrows, 0, 1));
相关问题