打印多维数组的单个项目

我的多维数组有问题。我在这里遇到两个问题:打印数组时,每个对象两次。我的下一个问题是,例如,我想获得一列的结果:[strProductNaam] =>可调哑铃-Bowflex 552i-2至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
        )
评论
  • hipsum
    hipsum 回复

    in sqlsrv_fetch_array pass SQLSRV_FETCH_ASSOC as the 2nd argument. this will return only the named keys, and drop the numeric.

    while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
        $datas[] = $row['strProductNaam'];
    }