PHP从内部数组循环中添加数组项

时间:2011-08-22 19:02:39

标签: php arrays

我正在尝试构建一系列包含点菜商品和套餐优惠的购物车商品。我想从包中提取产品并将产品输入我的订单表。以下是我所拥有的。它会提取数据,但只会将内部数组(包产品)的最后一条记录添加到外部数组中。点菜项目很好。

$sql = "SELECT id, item_id, package
        FROM cart WHERE user_id = $userId";
$result = dbQuery($sql);

$arrCartContent = array();

while ($row = dbFetchAssoc($result)) {
    if ($row['package'] == 1) {

        // Add Product Credits to Company and Order
        $arrProductData = fetchProductDataByPackage($row['item_id']);

        $numItem = count($arrProductData);    
        if ($numItem > 0) {                        
            for ($i = 0; $i < $numItem; $i++) {
                extract($arrProductData[$i]);

                $row['product_id'] = $product_id;
                $row['description'] =  $pack_product_name;
            }
        }

    } else {
        $itemDetails = fetchProductDetails($row['item_id']);

        $row['product_id'] = $row['item_id'];
        $row['description'] = $itemDetails['name'];
    }
    $arrCartContent[] = $row;
}

if(!empty($arrCartContent)) {
    foreach ($arrCartContent as $cartData) {

        $product_id = $cartData['product_id'];
        $description = $cartData['description'];

        $sqlInsert      = "INSERT INTO shop_order_x_product 
                            (order_id, product_id, description)
                         VALUES 
                            ($orderId, $product_id, '$description')";

        $resultInsert = dbQuery($sqlInsert) or die('Cannot add order products: ' . mysql_error());
    }
}

2 个答案:

答案 0 :(得分:0)

每次迭代都会覆盖$ row数组元素。尝试...

if ($row['package'] == 1) {

    // Add Product Credits to Company and Order
    $arrProductData = fetchProductDataByPackage($row['item_id']);

    $numItem = count($arrProductData);    
    if ($numItem > 0) {                        
        for ($i = 0; $i < $numItem; $i++) {
            extract($arrProductData[$i]);

            $row['product_id'] = $product_id;
            $row['description'] =  $pack_product_name;
            $arrCartContent[] = $row;
        }
    }

} else {
    $itemDetails = fetchProductDetails($row['item_id']);

    $row['product_id'] = $row['item_id'];
    $row['description'] = $itemDetails['name'];
    $arrCartContent[] = $row;
}

答案 1 :(得分:0)

你混淆了一下,你的一般方法很好,也许精简它会使这变得轻而易举并阻止你覆盖循环中的$row变量:

$arrCartContent = array();

while ($row = dbFetchAssoc($result))
{
    if ($row['package'] == 1)
    {    
        // Add Product Credits to Company and Order
        $arrProductData = fetchProductDataByPackage($row['item_id']);
        foreach($arrProductData as $productData)
        {
           $new = array();
           $new['product_id'] = $productData['product_id'];
           $new['description'] = $productData['description'];
           $arrCartContent[] = $new;
        }    
    }
    else
    {
        $itemDetails = fetchProductDetails($row['item_id']);
        $new = array();
        $new['product_id'] = $row['item_id'];
        $new['description'] = $itemDetails['name'];
        $arrCartContent[] = $new;
    }
}