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

时间:2014-08-07 12:23:26

标签: php sql arrays

我正在尝试构建一个显示产品价格的表格,但我无法弄清楚如何根据关键值匹配数据。

我查询SQL数据库并获得以下结果。日期实际上是月份

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

.etc

将sql结果读入数组。与表格示例类似。

现在我正在尝试制作一张显示全年名称和价格的表格,但我不知道如何做到这一点。这个想法如下。

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

应该有更好的方法来实现这一目标并让它真正发挥作用,但对于我的死我无法想出任何......

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

如果数组按产品名称的字母顺序排序,那么您可以使用此代码轻松实现您想要的效果

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

第二,当您将价格保存到其他$some_array时,同时存在相同的产品(需要按产品名称对数组进行排序)。如果某个月没有行,则价格为0.最后,您回显价格,并将$i的值分配给$j,以便下一个循环计算下一个产品的价格

答案 1 :(得分:0)

试试这个

$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>";
相关问题