Jump to content

Adding variables from foreach into new array structure for PHPExcel format


thenorman138

Recommended Posts

I'm currently looping on an array with this structure:

    Categories{
        CategoryName
        CategoryCode
        CategoryDescription

        Products{
            product_info{
                product_code
                product_type{
                    CountNumber
                    ProductDescription
                }
            }
            prices{
            "wholesale":"250",
            "retail":"400"
            }
        }    
    }

   

I'm looping on the above array at each level, and I've declared an array so that I can put all my needed values into it

    $priceResult = array();

    foreach($prices->categories as $category){ 
        $categoryName = $category->category_name;
        $categoryCode = $category->category_code;
        $categoryDescription = $category->category_desc;

        foreach($category->products as $product){

            foreach($product->product_info as $info){
                $product_code = $info->product_code;
                foreach($info->product_type as $type){
                    $CountNumber = $type->CountNumber;
                    $ProductDescription = $type->ProductDescription;
                }
            }
            foreach ($product->prices as $price => $amount) {
                $price_amount = $amount;
                
            }
        }
    }

The problem I'm having is I don't know how to properly push into that new ```$priceResult``` array so that I can use PHPExcel to put it into a format with a subheader.

The format I would want from the above example would be something like this

        Test Category 1  |  123  |   Category for Testing
        ==================================================
        PRD123 |  12  |  Product for Testing  |  150.00
        PRD112 |  17  |  Product for Testing  |  250.00

        Test Category 2  |  321  |   New Category for Testing
        =====================================================
        PRD189 |  16  |  Product for Testing  |  450.00
        PRD139 |  34  |  Product for Testing  |  350.00

So basically I want to call PHPExcel on my $priceResult array in order to get that format where I can list my category info, and for each product within that category, I'd have a product row. Everything would be grouped by the category main header

        $build = Excel::create($name, function ($excel) use ($priceResult) {

        $excel->setTitle('Test Products');

UPDATE:


SO I'm thinking my desired resulting array would be something like:

    CategoryCode : 123
    CategoryName : TestCategory
    CategoryDescription: For Testing
        Products{
            0{
                Product_code : 123,
                CountNumber : 12,
                ProductDescription: Test Product,
                price_amount : 150.00
            },
            1{
                Product_code : 112,
                CountNumber : 32,
                ProductDescription: Test Product 2,
                price_amount : 250.00
            }
        }


    
That way each category can be a header and all of its products can be rows for each category

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.