【问题标题】:Create multiple arrays in foreach loop php在foreach循环php中创建多个数组
【发布时间】:2018-03-30 05:42:08
【问题描述】:

我正在尝试从 foreach 循环创建多个数组,因此每次循环遍历项目时,它都会创建一个包含所有值的数组。然后我希望将这些值插入到主数组中。

所以本质上,我希望产品名称、ID、数量等在一个数组中,然后下一个项目在另一个数组中。然后这些将位于主 $cartinfo 数组中。

目前,当它遍历项目时,会将它们添加到单个数组中。谁能帮帮我?

function cart_items_array() { 

    $cartinfo = array();

    $carts2 = MultiCart\get_carts();

    foreach ( $carts2 as $cart2_id => $cart2 ) {
         // get array of items contained in a cart ...
        $items2 = MultiCart\get_cart( $cart_id2 );

        foreach ( $items2 as $item2_id => $item2 ) {

                $product_name = get_post($item2['product_id'])->post_title; 
                $familyterms = wp_get_post_terms( $item2['product_id'], 'pa_product-family'); 
                $cat_terms = wp_get_post_terms( $item2['product_id'], 'pa_product-category'); 
                $product_sku = get_post_meta( $item2['product_id'], '_sku', true );

                $cartinfo[] = $product_sku; 
                foreach ($cat_terms as $cat_term) { $cartinfo[] = $cat_term->name; };
                foreach ($familyterms as $family) { $cartinfo[] = $family->name; }; 
                $cartinfo[] = $product_name;
                $cartinfo[] = $item2['quantity'];
                $cartinfo[] = $cart2['name'];

        }
    } 

    return $cartinfo;
}

【问题讨论】:

    标签: php arrays foreach


    【解决方案1】:

    如果我错了,请纠正我。

    你想做这样的事情吗:

    function cart_items_array() { 
    
    $cartinfo = array();
    
    $carts2 = MultiCart\get_carts();
    
    // All the products
    $allTheCarts = array();
    
    foreach ( $carts2 as $cart2_id => $cart2 ) {
         // get array of items contained in a cart ...
        $items2 = MultiCart\get_cart( $cart_id2 );
    
        foreach ( $items2 as $item2_id => $item2 ) {
                $compProduct = array();
                $product_name = get_post($item2['product_id'])->post_title; 
                $familyterms = wp_get_post_terms( $item2['product_id'], 'pa_product-family'); 
                $cat_terms = wp_get_post_terms( $item2['product_id'], 'pa_product-category'); 
                $product_sku = get_post_meta( $item2['product_id'], '_sku', true );
    
                $cartinfo[] = $product_sku; 
                foreach ($cat_terms as $cat_term) { $cartinfo[] = $cat_term->name; };
                foreach ($familyterms as $family) { $cartinfo[] = $family->name; }; 
                $cartinfo[] = $product_name;
                $cartinfo[] = $item2['quantity'];
                $cartinfo[] = $cart2['name'];
    
                // Store the complete product info
                $allTheCarts[] = $cartinfo;
        }
    } 
    
    return $allTheCarts;
    }
    

    【讨论】:

    • 嗨,伙计,感谢您帮我看这个。我希望能够将每个产品存储在它自己的数组中,因此最终结果将如下所示: $allthecarts = array(array('product-name-1', 'product-id-1')array('product- name-2', product-id-2)) 等我尝试了你的建议,但它输出为 array();
    猜你喜欢
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多