我的多维数组有一个问题。这里有两个问题:当我打印我的数组时,我得到每个对象两次。我的下一个问题是,我想要得到一个列的结果,例如: strProductNaam =>可调哑铃-Bowflex552i-2 to 24 kg。有人能帮帮我吗?
提前感谢!
<?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
)发布于 2020-04-24 21:06:47
这就是解决方案:
while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
$datas[] = $row;
}echo '<pre>';
print_r($datas[1]['strProductNaam']);
echo '</pre>';发布于 2020-04-24 20:57:41
在sqlsrv_fetch_array中,将SQLSRV_FETCH_ASSOC作为第二个参数传递。这将只返回已命名的键,并删除数字。
while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
$datas[] = $row['strProductNaam'];
}https://stackoverflow.com/questions/61408825
复制相似问题