1

这是存储数据库值的数组:

$dataTitle[0] = "TIR";
$dataTitle[1] = "OIL";
$dataTitle[2] = "SPK";

$dataDesc[0] = "Tires";
$dataDesc[1] = "Oil";
$dataDesc[2] = "Spark Plugs";

$dataValue[0] = "100";
$dataValue[1] = "10";
$dataValue[2] = "4";

我可以使用以下方法手动将数据插入二维数组,但如果我要插入 100 条或更多行记录,它就没有用。这就是为什么下面需要 FOR 循环的原因。

$ResultView = array(array($dataTitle[0], $dataDesc[0], $dataValue[0]),
                array($dataTitle[1], $dataDesc[1], $dataValue[1]),
                    array($dataTitle[2], $dataDesc[2], $dataValue[2])
               );

如果使用下面的 FOR LOOP,则二维数组只存储最后一行记录,省略了第一行和第二行记录。

for ($i=0; $i<=2; $i++) {
          $ResultView = array(array($dataTitle[$i], $dataDesc[$i], $dataValue[$i])
               );
    }

当我为上面的 for 循环发出下面的输出脚本时,我得到了第 3 行的输出值并且缺少第 1 行和第 2 行结果。请帮忙!

for ($row=0; $row<=2; $row++) {

    for ($col=0; $col<=2; $col++) { 

        echo $ResultView[$row][$col]."&nbsp | ";
    }

    echo '<br />';
}

但是,我正在寻找一种使用 FOR 循环代替上述方法的方法。如何?

实际输出为:

Row 1 => TIR | Tires | 100 
Row 2 => OIL | Oil | 10 
Row 3 => SPK | Spark Plugs | 4

请指教。

4

6 回答 6

5

做了

 for ($i=0; $i<=2; $i++) {
      $ResultView[] = array(array($dataTitle[$i], $dataDesc[$i], $dataValue[$i])
           );
       }
于 2013-03-26T07:39:50.757 回答
1

这是最终的解决方案

  $dataTitle = array('TIR','OIL','SPK');
  $dataDesc=array('Tires','Oil','Spark Plugs'); 
 $dataValue=array('100','10','4');
 for ($i=0; $i<=2; $i++) { 
 $ResultView[] = array(array($dataTitle[$i], $dataDesc[$i], $dataValue[$i])); 
} 
     for ($row=0; $row<=2; $row++) 
     {   
         for ($col=0; $col<=2; $col++) 
         { 
              echo $ResultView[$row][0][$col]."  | "; 
          } 
   echo '<br />'; }
于 2013-03-26T08:23:22.700 回答
0

根据您的数组结构,您可以使用它

$data[0] = "TIR";
$data[1] = "Tires";
$data[2] = "100";

$data1[0] = "OIL";
$data1[1] = "Oil";
$data1[2] = "10";

$data2[0] = "SPK";
$data2[1] = "Spark Plugs";
$data2[2] = "4";

$resultCount = 3;  // adjust this value for the number of results you have
$products = array();

for ($i = 0; $i < $resultCount; $i++) {
    $products[] = ${'data' . ($i ? $i : '')};
}

print_r($products);
于 2013-03-26T07:30:13.773 回答
0

如果您的数据来自数据库:您可以使用while循环

$products        = array();
/* data from first table*/
while($dataTitle = mysql_fetch_assoc($sql)){
   $products[]   = $dataTitle;
} 

/* data from second table*/
while($dataDesc  = mysql_fetch_assoc($sql)){
   $products[]   = $dataDesc;
}

/* data from third table*/
while($dataValue = mysql_fetch_assoc($sql)){
   $products[]   = $dataValue;
}

echo "<pre>";
print_r($products);
于 2013-03-26T06:55:29.200 回答
0

如果您的数据库结果行作为数组返回,那么您应该能够遍历结果并将它们推送到您的产品数组中。

// assuming $results contains your DB response
$products = array();
for ($i = 0; $i < count($results); $i++) {
     $products[] = $results[$i];  // or whatever method you use to fetch a row
}

尽管您的数据库结果可能只是像多维数组一样使用。

于 2013-03-26T06:58:19.060 回答
0
**Method-1**  Raw method to generate the 2 diamentional array
This is your Array

$products = array( array( 'TIR', 'Tires', 100 ),
array( 'OIL', 'Oil', 10 ),
array( 'SPK', 'Spark Plugs', 4 ),
array( 'SPK', 'Spark Plugs', 4 ));

$cnt=0;
$cnt=count($products);
$product=array();

// 正在将数据复制到 $product 数组

for($i=0;$i<$cnt;$i++){
for($j=0;$j<3;$j++){
$product[$i][$j]= $products[$i][$j];
}
}


If you are getting data from data base 

for($i=0;$i<mysql_num_rows($result);$i++){
 $products[]=result [$i];
}
于 2013-03-26T07:14:43.810 回答