如何只显示具有匹配数组键的数组值?

I am trying to construct a table which shows price on product, but I cannot figure out how to match the data based on the key values.

I query the SQL db and get the following result. Date is actually the month

Product name  Price   date
care bear      11       4
care bear      12.5     5
care bear      9        6
car            123      3
car            344      4

.etc

The sql result is read to an array. Similar to table example.

Now I am trying to make a table that shows the name and price for the whole year, but I am stuck how to do this. The idea is the following.

    Product   jan feb mar apr may jun jul aug oct nov dec
    care bear  0   0   0  11  12.5  9  0   0   0   0   0
    car        0   0   3  4    0    0  0   0   0   0   0


// while there are products
    while (!empty($results[$k]['product'])) 
    {
// fill first cell with name.
echo '<td>'.$results[$k]['product'].'</td>';
// while the product has the same name
    while ($results[$i]['product'] = $results[$i-1]['product'] )
    {
//generate values for each month
        while ($i< 12) {
            if ($results[0]['month'] == $i){echo '<td>'.$results[0]['price'].'</td>';} 
                else if ($results[1]['month'] == $i){echo '<td>'.$results[1]['price'].'</td>';} 
                    else if ($results[2]['month'] == $i){echo '<td>'.$results[2]['price'].'</td>';}
                        else if ($results[3]['month'] == $i){echo '<td>'.$results[3]['price'].'</td>';}
                            else if ($results[4]['month'] == $i){echo '<td>'.$results[4]['price'].'</td>';}
                                else if ($results[5]['month'] == $i){echo '<td>'.$results[5]['price'].'</td>';}
                                    else if ($results[6]['month'] == $i){echo '<td>'.$results[6]['price'].'</td>';}
                                        else if ($results[7]['month'] == $i){echo '<td>'.$results[7]['price'].'</td>';}
                                            else if ($results[8]['month'] == $i){echo '<td>'.$results[8]['price'].'</td>';}
                                                else if ($results[9]['month'] == $i){echo '<td>'.$results[9]['price'].'</td>';}
                                                    else if ($results[10]['month'] == $i){echo '<td>'.$results[10]['price'].'</td>';}
                                                        else if ($results[11]['month'] == $i){echo '<td>'.$results[11]['price'].'</td>';}
                                                            else if ($results[12]['month'] == $i){echo '<td>'.$results[12]['price'].'</td>';}
                                                                //else if ($results[13]['month'] == $i){echo '<td>'.$results[13]['price'].'</td>';}
                                                                else echo '<td>0,00</td>';
            If($i==12)
            {
            echo '</tr><tr>';
            }
            $i++;
        }
    }
    $k++;
    }

There should be better ways of doing this and getting it actually to work, but for the death of me I can not come up with any...

Any help would be much appreciated.

If array is sorted alphabetically by product names then you can use this code to easily achieve what you want

$number_of_rows = count($results);
$i = 0;
while ($i < $number_of_rows) {
    $j = $i;
    $some_array = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    while ($results[$j]['product'] == $results[$i]['product']) {
        $some_array[$results[$j]['date']] = $results[$j]['price'];
        $j++;
    }
    echo '<tr><td>'.$results[$i]['product'].'</td>';
    for ($k = 1; $k <= 12; $k++)
        echo '<td>'.$some_array[$k].'</td>';
    echo '</tr>';
    $i = $j;
}

In second while you save prices to additional $some_array while there are the same products (it needs array to be sorted by product names). If there is no row for some month then the price is 0. At the end you echo the prices and assign to $i the value of $j so next loop will count prices of next product

try this

$arr_output = array(); 
foreach($results as $arr)
{
    $product = $arr['product'];
    $price = $arr['price'];
    $month = $arr['month'];

    $flag = false;
    foreach($arr_output as $key=>$arr_val)
    {
        if($arr_val['product']==$product)
        {
            $flag = true;
            $arr_output[$key][$month] = $price;
        }
    }

    if($flag===false)
    {
        $arr_temp['product'] = $product;
        foreach($i=1; $i<=12; $i++)
        {
            $arr_temp[$i] = 0;
        }
        $arr_temp[$month] = $price;
        $arr_output[] = $arr_temp;
    }
}

$arr_header = array("product", "Jan", "Feb", "March", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec");

echo "<table>";

echo "<tr>";
foreach($arr_header as $header_text)
{
    echo "<th>".$header_text."</th>";
}
echo "<tr>";

foreach($arr_output as $key=>$array)
{
    echo '<tr>';
        echo '<td>'.$array['product'].'</td>';
        for($i=1; $i<=12; $i++)
        {
            echo '<td>'.$array[$i].'</td>';
        }
    echo '</tr>';
}
echo "</table>";