英文:
I have many children arrays and i want all children arrays in single array in php
问题
$output = [];
function flattenArray($array) {
$result = [];
foreach ($array as $item) {
$result[] = [
'price' => $item['price'],
'weight' => $item['weight'],
];
if (isset($item['sub_option']) && is_array($item['sub_option'])) {
$result = array_merge($result, flattenArray($item['sub_option']));
}
}
return $result;
}
$output = flattenArray($arr);
print_r($output);
这段代码将帮助您将嵌套的数组转化为所需的扁平数组结构。
英文:
Hi i have many children arrays inside array all data exactly comes from api output result is i tried array recursive php but not worked
Array
(
[0] => Array
(
[price] => 100
[weight] => 42
[sub_option] => Array
(
[0] => Array
(
[price] => 25.99
[points] => 32
[sub_option] => Array
(
[0] => Array
(
[price] => 27.99
[weight] => 83
[sub_option] => Array
(
[0] => Array
(
[price] => 14.99
[weight] => 50
[sub_option] => NULL
)
)
)
)
)
[1] => Array
(
[price] => 24.99
[weight] => 25
[sub_option] => NULL
)
)
)
[1] => Array
(
[price] => 100
[weight] => 4233
[sub_option] => NULL
)
)
like this but i want output like this ......
Array
(
[0] => Array
(
[price] => 100
[weight] => 42
)
[1] => Array
(
[price] => 25.99
[points] => 32
)
[2] => Array
(
[price] => 27.99
[weight] => 83
)
[3] => Array
(
[price] => 14.99
[weight] => 50
[sub_option] => NULL
)
[4] => Array
(
[price] => 24.99
[weight] => 25
[sub_option] => NULL
)
[5] => Array
(
[price] => 100
[weight] => 4233
[sub_option] => NULL
)
)
I am using PHP version 7 and i also tried to search many online solutions from stackoverflow but i cant find please help me thanks
I tried this code but not working
$output = [];
$arr1 = [];
$arr2 = [];
foreach($arr as $key=>$value){
$arr1 = [
'price' => $value['price'],
'weight' => $value['weight'],
];
if(isset($value['sub_option']) && is_array($value['sub_option'])){
foreach($value['sub_option'] as $val2){
$arr2 = [
'price' => $val2['price'],
'weight' => $val2['weight'],
];
}
}
$output[] = $arr1+$arr2;
}
print_r($output);
答案1
得分: 1
这里是另一种执行此操作的方法(使用匿名函数):
#示例日期
$data = [
[
'price' => 101,
'weight' => 42,
'sub_option' =>
[
'price' => 101,
'weight' => 43,
'sub_option' => [
[
'price' => 101,
'weight' => 44,
'sub_option' => null
]
]
]
],
[
'price' => 102,
'weight' => 42,
'sub_option' => null
]
];
#全局收集器
$result = [];
#函数变量,这里值为null,以便我们可以将其用作引用
$func = null;
#创建真实函数并使用引用
$func = function ($current) use (&$result, &$func) {
#查找关联数组键
if (isset($current['price'])) {
#复制并删除不需要的键
$tmp = $current;
unset($tmp['sub_option']);
#收集
$result[] = $tmp;
#检查子数组并深入
if (is_array($current['sub_option'])) {
$func($current['sub_option']);
}
} elseif (is_array($current)) {
#迭代索引数组
foreach ($current as $sub) {
$func($sub);
}
}
};
$func($data);
var_export($result);
不会详细解释此代码,因为应该首先采纳来自GrumpyCrouton的第一个答案。但是,当您花一些时间阅读时,您可能会从此小代码片段中学到新的知识,以及如何在PHP中运作。
英文:
Here is another method to doing this (with anonymous function)
#example date
$data = [
[
'price' => 101,
'weight' => 42,
'sub_option' =>
[
'price' => 101,
'weight' => 43,
'sub_option' => [
[
'price' => 101,
'weight' => 44,
'sub_option' => null
]
]
]
],
[
'price' => 102,
'weight' => 42,
'sub_option' => null
]
];
#global collector
$result = [];
#function variable, here with null value so we can use it as reference
$func = null;
#create the real function & use references
$func = function ($current) use (&$result, &$func) {
#look for assoc array key
if (isset($current['price'])) {
#copy and unset not needed keys
$tmp = $current;
unset($tmp['sub_option']);
#collect
$result[] = $tmp;
#check for subarray and go deeper
if (is_array($current['sub_option'])) {
$func($current['sub_option']);
}
} elseif (is_array($current)) {
#iterate index arrays
foreach ($current as $sub) {
$func($sub);
}
}
};
$func($data);
var_export($result);
I will not explain this in detail, because first answer from GrumpyCrouton should be taken/accepted. But when you take some time, may you learn new stuff and how php works from this little code snipped.
答案2
得分: 0
以下是代码的翻译部分:
这是一个执行此操作的方法
function format_array($arr) {
$tmp = [];
foreach($arr as $a) {
//将根值逐个传递给辅助函数
//同时传递对$tmp的引用
format_array_helper($a, $tmp);
}
return $tmp;
}
function format_array_helper($loop_value, &$tmp) {
//将子选项设置为它自己的变量,
//从主变量中取消子选项
$sub_options = $loop_value['sub_option'];
unset($loop_value['sub_option']);
//将主变量添加到$tmp
$tmp[] = $loop_value;
//如果存在子选项
if(!empty($sub_options)) {
foreach($sub_options as $sub) {
//将每个子选项的根传递给辅助函数
//同时传递对tmp的引用
format_array_helper($sub, $tmp);
}
}
}
假设您的数组称为`$arr`。这是`print_r(format_array($arr));`的输出:
Array
(
[0] => Array
(
[price] => 100
[weight] => 42
)
[1] => Array
(
[price] => 25.99
[weight] => 32
)
[2] => Array
(
[price] => 27.99
[weight] => 83
)
[3] => Array
(
[price] => 14.99
[weight] => 50
)
[4] => Array
(
[price] => 24.99
[weight] => 25
)
[5] => Array
(
[price] => 100
[weight] => 4233
)
)
英文:
Here is one method of doing this
function format_array($arr) {
$tmp = [];
foreach($arr as $a) {
//pass each of the root values to the helper function
//also pass a reference to $tmp
format_array_helper($a, $tmp);
}
return $tmp;
}
function format_array_helper($loop_value, &$tmp) {
//set the sub options to it's own variable,
//unset the sub options from the main variable
$sub_options = $loop_value['sub_option'];
unset($loop_value['sub_option']);
//add the main variable to $tmp
$tmp[] = $loop_value;
//if there are sub options
if(!empty($sub_options)) {
foreach($sub_options as $sub) {
//pass each root of sub_options to the helper function
//also pass the reference to tmp
format_array_helper($sub, $tmp);
}
}
}
Let's assume your array is called $arr
. Here is the output of print_r(format_array($arr));
Array
(
[0] => Array
(
[price] => 100
[weight] => 42
)
[1] => Array
(
[price] => 25.99
[weight] => 32
)
[2] => Array
(
[price] => 27.99
[weight] => 83
)
[3] => Array
(
[price] => 14.99
[weight] => 50
)
[4] => Array
(
[price] => 24.99
[weight] => 25
)
[5] => Array
(
[price] => 100
[weight] => 4233
)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论