2016-01-29 63 views
1
$array[1] = array("Name1"=> array("2"=>"1460","3"=>"1868","4"=>"2158","5"=>"2537","6"=>"2915","8"=>"3673")); 
$array[2] = array("Prod2"=> array("3"=>"3079","4"=>"3625","5"=>"4172","6"=>"4718","8"=>"5811")); 
$cnt = 0; 
for ($x = 0; $x < 100; $x++) {  
    $product = $array[$x]; // dynamic all product`s - $name 
    foreach ($product as $good =>$massiv) { 
     $name = key($product);  // prod name 
     $proklkadok = array_keys($massiv); // prod prokladok 
     foreach ($massiv as $inner_key =>$price) { 
      echo "<tr><td>". $name. "</td>"; 
      $pr_val = array_keys($massiv); 
      echo "<td>".$pr_val[0]. "</td>"; 
      echo "<td>".$price."</td></tr>\r\n"; // product price 
     } 
    } 
} 

如何在表td單元格中打印$pr_val唯一? 這段代碼是錯誤的,壞表打印輸出:如何獲得不同的密鑰php數組表

Name1 2 1138 or wrong output too ... Name1 2,3,4,5,6,8 1138 
Name1 2 1868 or wrong output too ... Name1 2,3,4,5,6,8 1868 
Name1 2 2158 or wrong output too ... Name1 2,3,4,5,6,8 2158 
Prod2 3 3079 or wrong output too ... Prod2 2,3,4,5,6,8 3079 
Prod2 3 3625 or wrong output too ... Prod2 2,3,4,5,6,8 3625 
Prod2 3 4718 or wrong output too ... Prod2 2,3,4,5,6,8 4718  
Prod2 3 5811 or wrong output too ... Prod2 2,3,4,5,6,8 5811 

正確的輸出必須在HTML表

Name1 2 1138 
Name1 3 1868 
Name1 4 2158 
Name1 5 2537 
Name1 6 2915 
Name1 8 3673 

Prod2 3 3079 
Prod2 4 3079 
Prod2 5 3625 
Prod2 6 4718  
Prod2 8 5811 

請幫助我,我不能指望在輸出不同的計數陣列鍵表輸出

回答

0

這段代碼將輸出你的願望是在一個HTML表中的正確輸出什麼

$array[1]=array("Name1"=> array("2"=>"1460","3"=>"1868","4"=>"2158","5"=>"2537","6"=>"2915","8"=>"3673")); 
$array[2] =array("Prod2"=> array("3"=>"3079","4"=>"3625","5"=>"4172","6"=>"4718","8"=>"5811")); 
echo "<table>" . PHP_EOL; 
foreach($array as $product) {  
    foreach($product as $name =>$massiv) { 
     foreach($massiv as $inner_key => $price) { 
      echo "<tr><td>". $name. "</td>"; 
      echo "<td>".$inner_key. "</td>"; 
      echo "<td>".$price."</td></tr>" . PHP_EOL; // product price 
     } 
    } 
} 
echo "</table>" . PHP_EOL; 
+0

爲了使高品質的答案,你應該說明什麼問題是與原來的代碼,你改變來解決它。不要只是拋棄新的代碼而沒有解釋。 – Barmar

+0

eoe非常感謝!但不是全部,稍後見到我親愛的frined! – Alexey

2

bug是在

$pr_val = array_keys($massiv); 
echo "<td>".$pr_val[0]. "</td>"; 

array_key($massiv)回報massiv$pr_val[0]所有鍵始終返回他們的第一個(這恰好是其中最小的)。

嘗試

foreach($product as $good =>$massiv) { 

    foreach($massiv as $inner_key =>$price) { 
     echo "<tr><td>". $good. "</td>"; 

     echo "<td>".$inner_key. "</td>"; 
     echo "<td>".$price."</td></tr>\r\n"; // product price 
    } 
} 

應該輸出你的願望那到底是什麼。

+0

嘖嘖,非常感謝! – Alexey

0
$array = array(); 
$array[0] = array("Name1"=> array("2"=>"1460","3"=>"1868","4"=>"2158","5"=>"2537","6"=>"2915","8"=>"3673")); 
$array[1] = array("Prod2"=> array("3"=>"3079","4"=>"3625","5"=>"4172","6"=>"4718","8"=>"5811")); 
echo '<table rules="all" border="1">'; 
foreach($array as $v) { 
    foreach($v as $name => $ar) { 
     foreach($ar as $v1 => $v2) { 
      echo sprintf('<tr><td>%s</td><td>%s</td><td>%s</td></tr>'."\r\n", 
       $name, $v1, $v2); 
     } 
    } 
} 
echo '</table>'; 

通過Prizma