循环使用foreach并创建数组

时间:2018-02-07 13:32:33

标签: php arrays mysqli foreach

我正在尝试遍历属性数据库中的唯一区域数组,然后为每个区域的大小,值和建筑物大小创建一个数组。

我的问题是我可以创建数组,但是因为循环的每个foreach都具有相同名称的数组,所以这些数组不仅仅是该区域中的数量,而是先前通过区域循环的数量

例如: A区应该总共100 B区应该总计200

但守则正在回归: A区总计100 B区总计300(100 + 200)

问题可能出在数组,$ Value,$ ResSQFT和$ ComSQFT如何在每个foreach循环中使用相同的名称,但我知道如何在运行中重命名它们。

$Zones = "SELECT DISTINCT(ZONING_COD) FROM `ClevelandParcels`" ;

$ZonesStatus = mysqli_query($Connection, $Zones) ;
if(!$ZonesStatus)
{
  die('Could not get data: ' . mysqli_error());
}
while($row = mysqli_fetch_array($ZonesStatus))
{

$UniqueZones[] = array($row['ZONING_COD']);

}

echo "Zone,ParcelCount,ParcelValue,BuilingSqft,ResLivableArea,ComLeaseableArea<br>";

foreach ( $UniqueZones as $varZ ) {
  $ZoneParcelCount = "SELECT COUNT(*) FROM `ClevelandParcels` WHERE ZONING_COD =     '".$varZ[0]."'" ;
  $ZoneParcelCountStatus = mysqli_query($Connection, $ZoneParcelCount) ;
  $total = mysqli_fetch_row($ZoneParcelCountStatus);

  echo $varZ[0].",".$total[0];
mysqli_free_result($ZoneParcelCountStatus);

  $ZoneParcels = "SELECT * FROM `ClevelandParcels` WHERE ZONING_COD =     '".$varZ[0]."'" ;
  $ZoneParcelsStatus = mysqli_query($Connection, $ZoneParcels) ;
  if(!$ZoneParcelsStatus)
{
  die('Could not get data: ' . mysqli_error());
}

while ($row = mysqli_fetch_assoc($ZoneParcelsStatus)){
  $Value[] = $row['GROSS_CE_2'];
  $ResSqft[] = $row['TOTAL_RES_'];
  $ComSqft[] = $row['TOTAL_COM_'];
  }
    echo ",        $".array_sum($Value).",".array_sum($ResSqft).",".array_sum($ComSqft)."<br>";
    mysqli_free_result($ZoneParcelsStatus);

}

2 个答案:

答案 0 :(得分:0)

您必须重置数组才能在其中添加数据并计算总和:

$Value=[];
$ResSqft=[];
$ComSqft=[];
while ($row = mysqli_fetch_assoc($ZoneParcelsStatus)){
    $Value[] = $row['GROSS_CE_2'];
    $ResSqft[] = $row['TOTAL_RES_'];
    $ComSqft[] = $row['TOTAL_COM_'];
}

在您的代码中,每次都附加每个数组中的值,因此您的值会累积。

答案 1 :(得分:0)

$Zones = "SELECT DISTINCT(ZONING_COD) FROM `ClevelandParcels`" ;

$ZonesStatus = mysqli_query($Connection, $Zones) ;
if(!$ZonesStatus)
{
  die('Could not get data: ' . mysqli_error());
}
while($row = mysqli_fetch_array($ZonesStatus))
{

$UniqueZones[] = array($row['ZONING_COD']);

}

echo "Zone,ParcelCount,ParcelValue,BuilingSqft,ResLivableArea,ComLeaseableArea<br>";

foreach ( $UniqueZones as $varZ ) {
  $ZoneParcelCount = "SELECT COUNT(*) FROM `ClevelandParcels` WHERE ZONING_COD = '".$varZ[0]."'" ;
  $ZoneParcelCountStatus = mysqli_query($Connection, $ZoneParcelCount) ;
  $total = mysqli_fetch_row($ZoneParcelCountStatus);

  echo $varZ[0].",".$total[0];
mysqli_free_result($ZoneParcelCountStatus);

  $ZoneParcels = "SELECT * FROM `ClevelandParcels` WHERE ZONING_COD = '".$varZ[0]."'" ;
  $ZoneParcelsStatus = mysqli_query($Connection, $ZoneParcels) ;
  if(!$ZoneParcelsStatus)
{
  die('Could not get data: ' . mysqli_error());
}
unset($Value);
unset($ResSqft);
unset($ComSqft);
while ($row = mysqli_fetch_assoc($ZoneParcelsStatus)){
  $Value[] = $row['GROSS_CE_2'];
  $ResSqft[] = $row['TOTAL_RES_'];
  $ComSqft[] = $row['TOTAL_COM_'];
  }
    echo ", $".array_sum($Value).",".array_sum($ResSqft).",".array_sum($ComSqft)."<br>";
    mysqli_free_result($ZoneParcelsStatus);

}