(请不要介意标题,我不知道如何正确命名)我目前有一个多维数组,如下所示:
Array
(
[0] => Array
(
[French Navy] => Array
(
[84] => 2XL
[quantity] => 999
[product_colour_sizes_id] => 36786
[product_colour_sizes_product_id] => 1276
[product_colour_sizes_option_value_colour_id] => 182
[product_colour_sizes_option_value_size_id] => 80
[83] => XL
[82] => L
[81] => M
[80] => S
)
)
[1] => Array
(
[Classic Red] => Array
(
[84] => 2XL
[quantity] => 999
[product_colour_sizes_id] => 36780
[product_colour_sizes_product_id] => 1276
[product_colour_sizes_option_value_colour_id] => 178
[product_colour_sizes_option_value_size_id] => 80
[83] => XL
[82] => L
[81] => M
[80] => S
)
)
[2] => Array
(
[Burgundy] => Array
(
[84] => 2XL
[quantity] => 999
[product_colour_sizes_id] => 36774
[product_colour_sizes_product_id] => 1276
[product_colour_sizes_option_value_colour_id] => 106
[product_colour_sizes_option_value_size_id] => 80
[83] => XL
[82] => L
[81] => M
[80] => S
)
)
[3] => Array
(
[Bright Royal] => Array
(
[84] => 2XL
[quantity] => 999
[product_colour_sizes_id] => 36768
[product_colour_sizes_product_id] => 1276
[product_colour_sizes_option_value_colour_id] => 176
[product_colour_sizes_option_value_size_id] => 80
[83] => XL
[82] => L
[81] => M
[80] => S
)
)
[4] => Array
(
[Bottle Green] => Array
(
[84] => 2XL
[quantity] => 999
[product_colour_sizes_id] => 36762
[product_colour_sizes_product_id] => 1276
[product_colour_sizes_option_value_colour_id] => 86
[product_colour_sizes_option_value_size_id] => 80
[83] => XL
[82] => L
[81] => M
[80] => S
)
)
[5] => Array
(
[Black] => Array
(
[84] => 2XL
[quantity] => 999
[product_colour_sizes_id] => 36756
[product_colour_sizes_product_id] => 1276
[product_colour_sizes_option_value_colour_id] => 75
[product_colour_sizes_option_value_size_id] => 80
[83] => XL
[82] => L
[81] => M
[80] => S
)
)
)
我想要做的是将每次迭代减少到它自己的数组,因此我可以将键值作为名称访问(如果这是有意义的话)。像这样:
Array
(
[product_colour_sizes_id] => 36790
[product_colour_sizes_product_id] => 1276
[product_colour_sizes_option_value_colour_id] => 182
[product_colour_sizes_option_value_size_id] => 84
[quantity] => 999
[product_colour_sizes_name_sizes1] => 2XL => [id1] => 83
[product_colour_sizes_name_sizes2] => L => [id2] => 84
[product_colour_sizes_name_sizes3] => S => [id3] => 85
[product_colour_sizes_name_sizes4] => M => [id4] => 86
[product_colour_sizes_name_colours] => French Navy
)
我用来生成第一个数组的当前php代码是:
$colours = array();
$sizes = array();
$color = "";
$id = "";
$quantity = "";
//$count = count($colours);
foreach($product_combinations as $key => $product_combination){
if($key > -1){
if(
$product_combinations[$key+1]['product_colour_sizes_name_colours']
== $product_combinations[$key]['product_colour_sizes_name_colours']
){
$color = $product_combinations[$key]['product_colour_sizes_name_colours'];
$id = $product_combinations[$key]['product_colour_sizes_option_value_size_id'];
$sizes[$id] = $product_combinations[$key]['product_colour_sizes_name_sizes'];
$quantity = $product_combinations[$key]['quantity'];
$sizes['quantity'] = $quantity;
/* Dont forget theses*/
$sizes['product_colour_sizes_id'] = $product_combination['product_colour_sizes_id'];
$sizes['product_colour_sizes_product_id'] = $product_combination['product_colour_sizes_product_id'];
$sizes['product_colour_sizes_option_value_colour_id'] = $product_combination['product_colour_sizes_option_value_colour_id'];
$sizes['product_colour_sizes_option_value_size_id'] =$product_combination['product_colour_sizes_option_value_size_id'];
} else {
array_push($colours,
array(
$color => $sizes
));
unset($sizes);
}
}
}
我该怎么做?
答案 0 :(得分:1)
下面是将输入数组转换为单维数组的代码:
echo '<pre>';
$new_product_combinations = array();
foreach ($product_combinations as $k => $v) {
$array = array();
foreach ($v as $k1 => $v1) {
$i = 1;
foreach ($v1 as $k2 => $v2) {
if (is_int($k2)) {
$j = $i++;
$array['product_colour_sizes_name_sizes' . $j] = $v2;
$array['id' . $j] = $k2;
} else {
$array[$k2] = $v2;
}
}
$array['product_colour_sizes_name_colours'] = $k1;
}
print_r($array); // output single combination
$new_product_combinations[] = $array;
}
print_r($new_product_combinations);
以上代码将输出为:
Array
(
[product_colour_sizes_name_sizes1] => 2XL
[id1] => 84
[quantity] => 999
[product_colour_sizes_id] => 36786
[product_colour_sizes_product_id] => 1276
[product_colour_sizes_option_value_colour_id] => 182
[product_colour_sizes_option_value_size_id] => 80
[product_colour_sizes_name_sizes2] => XL
[id2] => 83
[product_colour_sizes_name_sizes3] => L
[id3] => 82
[product_colour_sizes_name_sizes4] => M
[id4] => 81
[product_colour_sizes_name_sizes5] => S
[id5] => 80
[product_colour_sizes_name_colours] => French Navy
)
:
Array
(
[0] => Array
(
[product_colour_sizes_name_sizes1] => 2XL
[id1] => 84
[quantity] => 999
[product_colour_sizes_id] => 36786
[product_colour_sizes_product_id] => 1276
[product_colour_sizes_option_value_colour_id] => 182
[product_colour_sizes_option_value_size_id] => 80
[product_colour_sizes_name_sizes2] => XL
[id2] => 83
[product_colour_sizes_name_sizes3] => L
[id3] => 82
[product_colour_sizes_name_sizes4] => M
[id4] => 81
[product_colour_sizes_name_sizes5] => S
[id5] => 80
[product_colour_sizes_name_colours] => French Navy
)
:
)
希望这会对你有所帮助。