Geoff_S
Geoff_S

Reputation: 5107

Transforming multidimensional array to 2d

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"
    ]
  ]

Upvotes: 0

Views: 38

Answers (1)

Nishanth Matha
Nishanth Matha

Reputation: 6081

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.

Upvotes: 1

Related Questions