简体   繁体   中英

Create multiple arrays in foreach loop php

I'm trying to create multiple arrays from a foreach loop so each time it loops through the items, it create an array with all the values in. I then want these to be inserted into aa main array.

So essentially, i want the product name, id, quantity etc to be in one array and then the next item to be in another one. These will then be within the main $cartinfo array.

At the moment, when it loops through the items it adds them to a single array. Can anyone help me please?

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

Correct me if I got it wrong.

Do you want to do something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM