0

I've successfully built a multidimensional array that dumps this:

0 => array:11 [▼
  "category_code" => "123"
  "category_name" => "Testing"
  "category_description" => "This is a test category"
  19738 => array:5 [▼
    "identifier" => "720368842943"
    "description" => Test Description One
    "count" => 4
    "details" => array:2 [▼
      0 => array:3 [▼
        "detail_code" => "2751"
        "detail_code2" => "43"
        "detail_specifier" => "Detail One"
      ]
      1 => array:3 [▼
        "detail_code" => "2681"
        "detail_code2" => "9"
        "detail_specifier" => "Detail Two"
      ]
    ]
    "prices" => array:1 [▼
      "01" => "1129.00"
    ]
  ]
  19739 => array:5 [▼
    "identifier" => "720368844121"
    "description" => "Test Description Two"
    "count" => 4
    "details" => array:2 [▼
      0 => array:3 [▼
        "detail_code" => "2751"
        "detail_code2" => "43"
        "detail_specifier" => "Detail One"
      ]
      1 => array:3 [▼
        "detail_code" => "2681"
        "detail_code2" => "9"
        "detail_specifier" => "Detail Two"
      ]
    ]
    "prices" => array:1 [▼
      "01" => "1490.00"
    ]
  ]

But for my purposes I actually need a 2d array where one element would be the category fields, then each item and all of its info would be another element so that I can export this as rows.

How can I make this a 2d array?

$allCategoryResult= array();

foreach($prices->categories as $category){ 
    $categoryItem = array(); 
    $categoryItem["category_code"] = $category->category_code;
    $categoryItem["category_name"] = $category->category_name; 
    $categoryItem["category_desc"] = $category->category_desc;

    foreach($category->skus as $sku){
        $skuItem = array(); 

        $skuItem["identifier"] = $sku->sku_info->identifier;
        $skuItem["description"] = $sku->sku_info->item->description;
        $skuItem["count"] = $sku->sku_info->item->item_type->count;

        $skuItem["details"] = array(); 
        foreach ($sku->sku_info->details as $details) {
            $detailsItem = array(); 
            $detailsItem["detail_code"] = $details->detail_code;
            $detailsItem["detail_code2"] = $details->detail_code2;
            $detailsItem["detail_specifier"] = $details->detail_specifier;
            $skuItem["details"][] = $detailsItem; 
        }

        $skuItem["prices"] = get_object_vars($sku->prices);


        $itemCode = $sku->sku_info->item->item_code;
        $categoryItem[$itemCode] = $skuItem; 
    }
    $allCategoryResult[] = $categoryItem; 
}

Example of expected output

0 => array: [▼
  "category_code" => "123"
  "category_name" => "Testing"
  "category_description" => "This is a test category"
  Array:[ 
    "item-code" => 19738 
    "identifier" => "720368842943"
    "description" => Test Description One
    "count" => 4
    "detail_code" => "2751"
    "detail_code2" => "43"
    "detail_specifier" => "Detail One"
    "detail_code" => "2681"
    "detail_code2" => "9"
    "detail_specifier" => "Detail Two"
    "01" => "1129.00"
    ]
  ]
7
  • Can you post expected output? It's not obvious to me how "details" should be organized, for example. Commented Jan 18, 2019 at 2:11
  • Yes one moment... Commented Jan 18, 2019 at 2:16
  • Updated now, something like that. Basically every attribute for products should be in one product array. So a category with 3 products would be 4 arrays that I could export as 4 rows Commented Jan 18, 2019 at 2:22
  • What you posted isn't possible in PHP. You have various duplicate keys in an array. Commented Jan 18, 2019 at 2:23
  • That's why I was hoping I could loop on details just to get each value, even if I don't have keys Commented Jan 18, 2019 at 2:27

1 Answer 1

1

All you need to do is don't nest the other details of sku in a separate array. Just push it to skuitems array Something like below

$allCategoryResult= array();

foreach($prices->categories as $category){ 
    $categoryItem = array(); 
    $categoryItem["category_code"] = $category->category_code;
    $categoryItem["category_name"] = $category->category_name; 
    $categoryItem["category_desc"] = $category->category_desc;

    foreach($category->skus as $sku){
        $skuItem = array(); 

        $skuItem["identifier"] = $sku->sku_info->identifier;
        $skuItem["description"] = $sku->sku_info->item->description;
        $skuItem["count"] = $sku->sku_info->item->item_type->count;

        $skuItem["details"] = array(); 
        foreach ($sku->sku_info->details as $details) {
            $detailsItem = array(); 
            $skuItem["detail_code"] = $details->detail_code;                
            $skuItem["detail_code2"] = $details->detail_code2;
            $skuItem["detail_specifier1"] = $details->detail_specifier1;
            $skuItem["detail_specifier2"] = $details->detail_specifier2;     
        }

        array_push($skuItem,$sku->prices);


        $itemCode = $sku->sku_info->item->item_code;
        $categoryItem[$itemCode] = $skuItem; 
    }
    $allCategoryResult[] = $categoryItem; 
}

P.S: Array key should be unique so, you can't have two keys called with same name "detail_specifier", so have modified it accordingly here but YMMV.

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

6 Comments

This makes sense, but sense my details values have the same index names could I do this without named keys?
nope then you wouldn't know what's the array is referring to I mean you would end up something like [0]=>'Detail One',[1]=>'Detail Two' which might not make sense at all
other option would be you could nest them together as array like : "detail_specifier"=>['Detail One', 'Detail Two']
But that wouldn't necessarily matter if I'm exporting this to excel as rows right? I don't need to associate anything at that point I just need a category array with all the following product arrays values to print as rows
if you don't want assosiative array and you want indexed array you could just do array_push($skuItem,$details->detail_code); instead of $skuItem["detail_code"] = $details->detail_code; and do the same for all elements
|

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.