PHP multidiamentional Array Flatten issue
one text
Solution:
You don't actually need to use recursion here. As long as your data structure is always the same, you only need to deal with one level.
<?php
$input = [
[
'ID' => 1,
'ProductName' => 'Test Product 1',
'ProductType' => 'single',
'Pricing' => [
'whole_sale_price' => 50,
'retail_price' => 55,
'store_price' => 52
]
],
[
'ID' => 2,
'ProductName' => 'Test Product 2',
'ProductType' => 'single',
'Pricing' => [
'whole_sale_price' => 60,
'retail_price' => 65,
'store_price' => 62
]
],
[
'ID' => 3,
'ProductName' => 'Test Product 3',
'ProductType' => 'single',
'Pricing' => [
'whole_sale_price' => 70,
'retail_price' => 75,
'store_price' => 72
]
]
];
function flattenProductArray($productArray)
{
// Loop through the top level elements in the product array - pass by reference so we can change the contents
foreach($productArray as &$currProduct)
{
// Loop through the elements in each product
foreach($currProduct as $subKey=>$subValue)
{
// If the value is an array
if(is_array($subValue))
{
// Merge the contents of the sub array into the product array
$currProduct = array_merge($currProduct, $subValue);
// Unset the sub array
unset($currProduct[$subKey]);
}
}
}
return $productArray;
}
$flat = flattenProductArray($input);
print_r($flat);
If you do need to support unknown levels of depth, you only want to apply the recursion to each element in the array, not the entire array itself. Here's how you can fix your function and use it:
<?php
$input = [
[
'ID' => 1,
'ProductName' => 'Test Product 1',
'ProductType' => 'single',
'Pricing' => [
'whole_sale_price' => 50,
'retail_price' => 55,
'store_price' => 52,
'foo' => [
'bar' => 'baz',
'tree' => [
'wombats' => 23,
'kangaroos' => 57
]
]
]
],
[
'ID' => 2,
'ProductName' => 'Test Product 2',
'ProductType' => 'single',
'Pricing' => [
'whole_sale_price' => 60,
'retail_price' => 65,
'store_price' => 62,
'foo' => [
'bar' => 'blerg',
'tree' => [
'wombats' => 87,
'kangaroos' => 99
]
]
]
],
[
'ID' => 3,
'ProductName' => 'Test Product 3',
'ProductType' => 'single',
'Pricing' => [
'whole_sale_price' => 70,
'retail_price' => 75,
'store_price' => 72,
'foo' => [
'bar' => 'fleem',
'tree' => [
'wombats' => 12,
'kangaroos' => 34
]
]
]
]
];
function array_flatten($a)
{
// Output buffer
$entry = [];
// Loop through the top level of the array
foreach ($a as $key => $el)
{
// If the current element is an array
if (is_array($el))
{
// Call the function recursively
$flat = array_flatten($el);
// Place the contents of the flattened array into the output buffer
foreach ($flat as $currFlatKey => $currFlatValue)
{
$entry[$currFlatKey] = $currFlatValue;
}
}
else // Simply pass the element through to the output buffer
{
$entry[$key] = $el;
}
}
return $entry;
}
// Create a buffer for the flattened results
$flat = [];
// Loop through the top level elements and flatten each
foreach ($input as $currProduct)
{
// Append the current flattened product element to the buffer
$flat[] = array_flatten($currProduct);
}
print_r($flat);
Keep in mind that when you flatten an array, any keys that are common among the different levels will have their values overwritten by the innermost value with that key unless you implement some sort of logic to prevent that.
$input = [
[
'ID' => 1,
'ProductName' => 'Test Product 1',
'ProductType' => 'single',
'Overwritten' => 'Outer value',
'Pricing' => [
'whole_sale_price' => 50,
'retail_price' => 55,
'store_price' => 52,
'Overwritten' => 'Inner value',
]
]
];
function flattenProductArray($productArray)
{
foreach ($productArray as &$currProduct)
{
foreach ($currProduct as $subKey => $subValue)
{
if (is_array($subValue))
{
$currProduct = array_merge($currProduct, $subValue);
unset($currProduct[$subKey]);
}
}
}
return $productArray;
}
$flat = flattenProductArray($input);
assert(($flat[0]['Overwritten'] == 'Inner value'), 'Outer value will be overwritten by inner value');
print_r($flat);
Source