1

I currently have an array that is building with the correct data by looping an object but it's giving the incorrect format:

$priceResult = array();

foreach($prices->categories as $category){ 

    $priceResult[] = $category->category_name;
    $priceResult[] = $category->category_desc;
    $priceResult[] = $category->category_code;

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

        $priceResult[] = $product->product_info->item->item_code;

        foreach ($product->product_info->details as $details) {

            $priceResult[] = $details->description;
            $priceResult[] = $details->color;
            $priceResult[] = $details->sector;
        }

        $priceResult[] = $product->product_info->code;
        $priceResult[] = $product->product_info->item->description;
        $priceResult[] = $product->product_info->item->item_type->quantity;

        foreach(get_object_vars($product->prices) as $amount){

            $priceResult[] = $amount;

        }
    }
}

This isn't associative though.

So currently, say I have one category with two products then they all print out as a single array

array({
    1:category_name
    2:category_desc
    3:category_code
    4:item_code
    5:description
    6:color
    7:sector
    8:code
    9:description
    10:quantity
    11:amount
    12:item_code
    13:description
    14:color
    15:sector
    16:code
    17:description
    18:quantity
    19:amount
})

I'd like to get a structure where the parent level is the category_code with it's name and description, then each item_code and their own info like so:

array({
    category_name
    category_desc
    category_code
      Array(
       1: item_code array(
            details array(
                description
                color
                sector
            )
            code
            description
            quantity
            amount) 
       2: item_code array(
            details array(
                description
                color
                sector
            )
            code
            description
            quantity
            amount) 
 ) 
})

How can I modify this to create the levels like I need so that it formats properly when I export to a spreadsheet

1 Answer 1

1

You need to split you code and init new object in the loop.

Consider the following (notice the comment in the code)

$allCategoryResult= array(); // init at first - notice naming as category and not prices

foreach($prices->categories as $category){ 
    $categoryItem = array(); // as current category to populate

    // give name to the keys and not just numbers
    $categoryItem["name"] = $category->category_name; 
    $categoryItem["desc"] = $category->category_desc;
    $categoryItem["code"] = $category->category_code;

    foreach($category->products as $product){
        $productItem = array(); // new product, so init new array for him

        // fill all the item data with name - maybe you will need to fix the paths here
        $productItem["details"] = array(); // init empty array for all the details elements
        foreach ($product->product_info->details as $details) {
            $detailsItem = array(); // init details array for each detail element
            $detailsItem["description"] = $details->description;
            $detailsItem["color"] = $details->color;
            $detailsItem["sector"] = $details->sector;
            $productItem["details"][] = $detailsItem; // add the detail element to the product array
        }

        $productItem["code"] = $product->product_info->code;
        $productItem["itemDescription"] = $product->product_info->item->description;
        $productItem["quantity"] = $product->product_info->item->item_type->quantity;
        $productItem["amount"] = get_object_vars($product->prices);

        $itemCode = $product->product_info->item->item_code;
        categoryItem[$itemCode] = $productItem; // add the product to category array by his code
    }
    $allCategoryResult[] = $categoryItem; //add the category to all category array
}

Writing this without see you actual data is pretty hard - so I guess you will have to modify it to fit your data.

But I hop you get the idea. Good luck!

Sign up to request clarification or add additional context in comments.

9 Comments

I think this works but one thing I noticed is that details has 2 sets for every itemCode. So wouldn't I need another foreach around details?
Yes - I was not aware of that. You need another for loop - I will updated my post shortly to support it
Thanks so much for that
Yes that's it! Thanks again
So this dumps the data structure I want and I export to xlsx it shows each category_name, category_desc, and category_code. However, I was hoping it would do the category data in one row and each associated item (itemCode data) as a row beneath. Will that not work like this?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.