在assotiave数组中添加产品

时间:2018-03-08 15:01:32

标签: javascript php

我遇到了问题。在某个网站,我必须显示买家购买了哪些产品。我还采用了产品的每个类别,将其放入assotiave数组中,然后将其放入变量中。当我添加同一类别的产品时,一切运作良好。但是,当我添加不同类别的产品时,存在问题。我的问题是:有没有人知道如何从现有代码中将不同类别的产品添加到同一个数组中?

            foreach ($acne_arr as $key => $value) {
                $message_e .= '<tr>
                    <td style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#000;height:30px;" align="center"></td>
                    <td style=" font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#000;height:30px;">'.$value['PRODUCT'].'</td>
                    <td style="font-family:Arial, Helvetica, sans-serif; text-align:center; text-transform:uppercase;font-size:14px;color:#000;height:30px;" align="center">'.$value['price'].'</td>
                    <td style="font-size:12px; color:#000; font-family:Arial, Helvetica, sans-serif;height:30px;">
                        <input type="text" style="width:37px; background:none;border:none;font-weight:normal;" value="'.$value['qty'].'" readonly />
                    </td>
                    <td style="font-size:12px; color:#000; font-family:Arial, Helvetica, sans-serif;height:30px;">
                        <input style="width:37px; background:none; border:none; font-weight:normal;" type="text" value="'.$value['amt'].'" readonly />
                    </td>
                </tr>';
                  $product = json_encode($acne_arr);

               }
                    foreach ($acb_arr as $key => $value) {
                $message_e .= '<tr>
                    <td style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#000;height:30px;" align="center"></td>
                    <td style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#000;height:30px;">'.$value['PRODUCT'].'</td>
                    <td style="font-family:Arial, Helvetica, sans-serif; text-align:center; text-transform:uppercase;font-size:14px;color:#000; height:30px;" align="center">'.$value['price'].'</td>
                    <td style="font-size:12px; color:#000; font-family:Arial, Helvetica, sans-serif;height:30px;">
                        <input type="text" style="width:37px; background:none;border:none;font-weight:normal;" value="'.$value['qty'].'" readonly />
                    </td>
                    <td style="font-size:12px; color:#000; font-family:Arial, Helvetica, sans-serif;height:30px;">
                        <input style="width:37px; background:none; border:none; font-weight:normal;" type="text" value="'.$value['amt'].'" readonly />
                    </td>
                </tr>';
                $product1 = json_encode($acb_arr);
                $product = $product1;
              }
foreach ($abh_arr as $key => $value) {
                $message_e .= '<tr>
                    <td style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#000;height:30px;" align="center"></td>
                    <td style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#000;height:30px;">'.$value['PRODUCT'].'</td>
                    <td style="font-family:Arial, Helvetica, sans-serif; text-align:center; text-transform:uppercase; font-size:14px; color:#000; height:30px;" align="center">'.$value['price'].'</td>
                    <td style="font-size:12px; color:#000; font-family:Arial, Helvetica, sans-serif;height:30px;">
                        <input type="text" style="width:37px; background:none;border:none;font-weight:normal;" value="'.$value['qty'].'" readonly />
                    </td>
                    <td style="font-size:12px; color:#000; font-family:Arial, Helvetica, sans-serif;height:30px;">
                        <input style="width:37px; background:none; border:none; font-weight:normal;" type="text" value="'.$value['amt'].'" readonly />
                    </td>
                </tr>';
                $product2 = json_encode($abh_arr);
                $product .= $product2;
                }

提前谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用一个array添加许多类别的产品,例如

$books = array(1,2,3,4);
$pens = array(1,2,3,4);
$others = array(1,2,3,4);
$mainArray = array(
    'books' => $books,
    'pens' => $pens,
    'others' => $others
);

如果您需要在任何类别中添加新产品

$mainArray['books'][] = $newBook;
$mainArray['pens'][] = $newPen;

它会将新产品添加到新的免费索引