我的多维数组有问题。我有两个问题。当我打印数组时,每个对象都得到两次。我的下一个问题是,我想得到一列的结果,例如:[strProductNaam] => Adjustable Dumbbells - Bowflex 552。[strProductNaam] => 可调节哑铃 - Bowflex 552i - 2至24公斤。谁能帮我解决这个问题?
先谢谢你
<?php
require('php/connection.php');
$sql = "SELECT * FROM tblProduct";
$result = sqlsrv_query($conn,$sql);
if( $result === false ) {
die( print_r( sqlsrv_errors(), true));
}
while($row = sqlsrv_fetch_array($result)) {
$datas[] = $row;
}
echo '<pre>';
print_r($datas);
echo '</pre>';
foreach($datas as $data){
echo $data['strProductNaam'] ." ";
}
sqlsrv_free_stmt($result);
sqlsrv_close($conn);
?>
结果后:print_r($datas)。
Array
(
[0] => Array
(
[0] => 1
[ID] => 1
[1] => 1
[CategorieID] => 1
[2] => Adjustable Dumbbells - Bowflex 552i - 2 to 24 kg
[strProductNaam] => Adjustable Dumbbells - Bowflex 552i - 2 to 24 kg
[3] => 499
[intPrijs] => 499
[4] => Easy to adjust
[strPlusPunt1] => Easy to adjust
[5] => Saving space
[strPlusPunt2] => Saving space
)
[1] => Array
(
[0] => 2
[ID] => 2
[1] => 1
[CategorieID] => 1
[2] => Dumbbell 15kg
[strProductNaam] => Dumbbell 15kg
[3] => 28.95
[intPrijs] => 28.95
[4] => Easy to expand
[strPlusPunt1] => Easy to expand
[5] => Easily adjustable
[strPlusPunt2] => Easily adjustable
)
在 sqlsrv_fetch_array
俓 SQLSRV_FETCH_ASSOC
作为第2个参数,这将只返回命名键,而放弃数字键。
while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
$datas[] = $row['strProductNaam'];
}
这就是解决方案。
while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
$datas[] = $row;
}
echo '<pre>';
print_r($datas[1]['strProductNaam']);
echo '</pre>';