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);
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.