本文介绍了从mulitdimensional阵列PHP地带层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些困难的操纵之下阵列。我曾尝试过各种东西,我觉得应该有一个简单的方法来实现这一点,但我似乎无法弄清楚。任何提示或帮助很大AP preciated,谢谢!
阵列
(
[0] =>排列
(
[0] =>排列
(
[颜色] =>蓝色
) [1] =>排列
(
【尺寸】= GT;小号
) ) [1] =>排列
(
[0] =>排列
(
[颜色] =>蓝色
) [1] =>排列
(
【尺寸】= GT;中号
) )
)
我希望的结果是:
阵列
(
[0] =>排列
(
[颜色] =>蓝色
【尺寸】= GT;小号
) [1] =>排列
(
[颜色] =>蓝色
【尺寸】= GT;中号
)
)
解决方案
这个作品:
$项目=阵列(
阵列(
阵列(颜色=>中蓝),
阵列(大小=>中S)
)
阵列(
阵列(颜色=>中蓝),
阵列(大小=>中M)
)
);
$ new_items =阵列();
的foreach($项目为$项目){
$收集=阵列();
的foreach($项目为$值){
的foreach($值$关键=> $值){
$收集[$关键] = $价值;
}
}
$ new_items [] = $收集;
}
后续代码var_dump($ new_items);
您数组是3深,那么你需要至少3个嵌套的循环。
输出:
阵列(2){
[0] =>
阵列(2){
[颜色] =>
串(4)蓝
[大小] =>
串(1)的S
}
[1] =>
阵列(2){
[颜色] =>
串(4)蓝
[大小] =>
串(1)的M
}
}
I am having some difficulty manipulating the below array. I have tried various things and I feel like there should be a simple way to achieve this, but I cannot seem to figure it out. Any tips or help greatly appreciated, thanks!
Array
(
[0] => Array
(
[0] => Array
(
[Color] => Blue
)
[1] => Array
(
[Size] => S
)
)
[1] => Array
(
[0] => Array
(
[Color] => Blue
)
[1] => Array
(
[Size] => M
)
)
)
My desired outcome is:
Array
(
[0] => Array
(
[Color] => Blue
[Size] => S
)
[1] => Array
(
[Color] => Blue
[Size] => M
)
)
解决方案
This works:
$items = array(
array(
array("Color" => "Blue"),
array("Size" => "S")
),
array(
array("Color" => "Blue"),
array("Size" => "M")
)
);
$new_items = array();
foreach ($items as $item) {
$collect = array();
foreach ($item as $values) {
foreach ($values as $key => $value) {
$collect[$key] = $value;
}
}
$new_items[] = $collect;
}
var_dump($new_items);
Your array is 3 deep, so you need at least 3 nested loops.
Output:
array(2) {
[0]=>
array(2) {
["Color"]=>
string(4) "Blue"
["Size"]=>
string(1) "S"
}
[1]=>
array(2) {
["Color"]=>
string(4) "Blue"
["Size"]=>
string(1) "M"
}
}
这篇关于从mulitdimensional阵列PHP地带层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!