PHP多维数组扁平化问题



我正在尝试压平我的多维数组,但它对我不起作用,我尝试了Stack溢出上所有可用的解决方案。我的目标是首先将数组展平,然后将其导出为CSV,稍后我可以进行此操作。请为我提供参考或代码我的阵列示例

Array
(
[0] => Array
(
[ID] => 1
[ProductName] => Test Product 1
[ProductType] => single
[Pricing] => Array
(
[whole_sale_price] => 50
[retail_price] => 55
[store_price] => 52
)
)
[1] => Array
(
[ID] => 2
[ProductName] => Test Product 2
[ProductType] => single
[Pricing] => Array
(
[whole_sale_price] => 60
[retail_price] => 65
[store_price] => 62
)
)
[2] => Array
(
[ID] => 3
[ProductName] => Test Product 3
[ProductType] => single
[Pricing] => Array
(
[whole_sale_price] => 70
[retail_price] => 75
[store_price] => 72
)
)
)

输出我正试图期待

Array
(
[0] => Array
(
[ID] => 1
[ProductName] => Test Product 1
[ProductType] => single
[whole_sale_price] => 50
[retail_price] => 55
[store_price] => 52
)
[1] => Array
(
[ID] => 2
[ProductName] => Test Product 2
[ProductType] => single
[whole_sale_price] => 60
[retail_price] => 65
[store_price] => 62
)
[2] => Array
(
[ID] => 3
[ProductName] => Test Product 3
[ProductType] => single
[whole_sale_price] => 70
[retail_price] => 75
[store_price] => 72
)
)

我的PHP代码

function array_flatten($a, $flat = []) {
$entry = [];
foreach ($a as $key => $el) {
if (is_array($el)) {
$flat = array_flatten($el, $flat);
} else {
$entry[$key] = $el;
}
}
if (!empty($entry)) {
$flat[] = $entry;
}
return $flat;
}

您实际上不需要在这里使用递归。只要你的数据结构总是一样的,你只需要处理一个级别。

<?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);

如果确实需要支持未知的深度级别,则只需将递归应用于数组中的每个元素,而不是整个数组本身。以下是如何修复您的功能并使用它:

<?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);

请记住,当您展平数组时,不同级别中常见的任何键的值都将被该键的最里面的值覆盖,除非您实现某种逻辑来防止这种情况发生。

$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);

最新更新