PHP数组结果-分配给自己的变量


PHP Array results - assign to their own variable

嗨,这是我的查询结果

transsum
-19121111
-17432222
-19873333
-22404444
-21955555
-19716666

我需要将每个结果放入它自己的变量中

我有这个,但我认为它不对

$arr_results = odbc_exec($TD_DB_RESOURCE, $query);
foreach ($row = odbc_fetch_array($arr_results) )  
{
    $price0 = $row[0];
    $price1 = $row[1];
    $price2 = $row[2];
    $price3 = $row[3];
    $price4 = $row[4];
    $price5 = $row[5];
}

更新代码

$TD_DB_RESOURCE = open_teradata_resource();
$arr_results = odbc_exec($TD_DB_RESOURCE, $query);
$rows = array();
$i=0;
while ($myRow = odbc_fetch_array($arr_results) )  
{
$rows[$i] = $myRow;
$i++;
}

输出

Array ( [0] => Array ( [TOTAL] => -19126241 ) [1] => Array ( [TOTAL] => -17439360 ) [2] => Array ( [TOTAL] => -19871999 ) [3] => Array ( [TOTAL] => -22409254 ) [4] => Array ( [TOTAL] => -21950605 ) [5] => Array ( [TOTAL] => -19710526 ) ) 

如果您想要的是一个带有总计的数组,您可以使用以下方法:

$prices = [];
while ($myRow = odbc_fetch_array($arr_results)) {
    $prices[] = $myRow['TOTAL'];
}

之后,$prices的内容应该是:

[-19126241, -17439360, ...]

您可以使用大括号动态创建变量:

$arr_results = odbc_exec($TD_DB_RESOURCE, $query);
$count = 0;
foreach ($row = odbc_fetch_array($arr_results)) {
    $price{$count} = $row;
    $count++;
 }

您可以执行以下操作:

<?php
    $arr_result ...
    $index=0;
    foreach($row = odbc_fetch_array($arr_results)){
        ${'price'.$index++} = $row;
    }
$arr_results = odbc_exec($TD_DB_RESOURCE, $query);
$i=0;
$prices = array(); //Make a dynamic array of elements
foreach ($row = odbc_fetch_array($arr_results) )  
{
  $prices[$i] = $row[$i];
  $i++;
}

然后在计算中的任何位置使用数组元素,如

$c = $price[0]+$price[1];