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
        }  
    }
}

how to print $pr_val unique in table td cell ? This code is wrong, bad table prints the output:

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  

Correct output must be in HTML TABLE

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 

pls help me, I can't count and output different count of array keys in the table output

3 Answers 3

2

The bug is at

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

array_key($massiv) returns all keys of massiv and $pr_val[0]always returns the first of them (which happens to be the smallest of them).

Try

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
    }  
}

That should output exactly what you desire.

0
0

This code will output what you desire as the correct output in an HTML table

$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;
2
  • To make a high quality answer, you should explain what the problem was with the original code, and what you changed to fix it. Don't just dump new code with no explanation.
    – Barmar
    Commented Jan 29, 2016 at 23:21
  • eoe thanks thats great! but is not all, see you later my dear frined!!
    – Alexey
    Commented Jan 30, 2016 at 0:11
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>';

by Prizma

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.