Reputation: 1
How could I convert this array:
$a = array(
0 => array(
0 => 31,
1 => 39
),
1 => array(
0 => 41
)
);
to this one:
$a = array(
0 => array(
0 => array(
0 => 31,
1 => 41
),
1 => array(
0 => 39,
1 => 41
)
),
1 => array(
0 => array(
0 => 41,
1 => 31
),
1 => array(
0 => 41,
1 => 39
)
)
);
I tried several ways, but find no proper solution. At the moment my brain is overheated. So maybe someone has a solution for me.
Thanks @Manos.
Unfortunatly there are dynamic arrays. So these static function wont work for me.
So the array could also look like this:
$a = array(
0 => array(
0 => 31,
1 => 39
),
1 => array(
0 => 41,
1 => 49,
2 => 51
),
2 => array(
0 => 73
)
);
Output should look like this:
$a = array(
0 => array(
0 => array(
0 => 31,
1 => 41,
2 => 73
),
1 => array(
0 => 31,
1 => 49,
2 => 73
),
2 => array(
0 => 31,
1 => 51,
2 => 73
),
3 => array(
0 => 39,
1 => 41,
2 => 73
),
4 => array(
0 => 39,
1 => 49,
2 => 73
),
5 => array(
0 => 39,
1 => 51,
2 => 73
),
1 => array(
0 => array(
0 => 41,
1 => 31,
2 => 73
),
1 => array(
0 => 41,
1 => 39,
2 => 73
),
2 => array(
0 => 49,
1 => 31,
2 => 73
),
3 => array(
0 => 49,
1 => 39,
2 => 73
),
etc ......
)
);
Manos function Output:
array(3) {
[0]=>
array(8) {
[0]=>
array(2) {
[0]=>
int(31)
[1]=>
int(41)
}
[1]=>
array(2) {
[0]=>
int(39)
[1]=>
int(41)
}
[2]=>
array(2) {
[0]=>
int(31)
[1]=>
int(49)
}
[3]=>
array(2) {
[0]=>
int(39)
[1]=>
int(49)
}
[4]=>
array(2) {
[0]=>
int(31)
[1]=>
int(51)
}
[5]=>
array(2) {
[0]=>
int(39)
[1]=>
int(51)
}
[6]=>
array(2) {
[0]=>
int(31)
[1]=>
int(73)
}
[7]=>
array(2) {
[0]=>
int(39)
[1]=>
int(73)
}
}
[1]=>
array(9) {
[0]=>
array(2) {
[0]=>
int(41)
[1]=>
int(31)
}
[1]=>
array(2) {
[0]=>
int(49)
[1]=>
int(31)
}
[2]=>
array(2) {
[0]=>
int(51)
[1]=>
int(31)
}
[3]=>
array(2) {
[0]=>
int(41)
[1]=>
int(39)
}
[4]=>
array(2) {
[0]=>
int(49)
[1]=>
int(39)
}
[5]=>
array(2) {
[0]=>
int(51)
[1]=>
int(39)
}
[6]=>
array(2) {
[0]=>
int(41)
[1]=>
int(73)
}
[7]=>
array(2) {
[0]=>
int(49)
[1]=>
int(73)
}
[8]=>
array(2) {
[0]=>
int(51)
[1]=>
int(73)
}
}
[2]=>
array(5) {
[0]=>
array(2) {
[0]=>
int(73)
[1]=>
int(31)
}
[1]=>
array(2) {
[0]=>
int(73)
[1]=>
int(39)
}
[2]=>
array(2) {
[0]=>
int(73)
[1]=>
int(41)
}
[3]=>
array(2) {
[0]=>
int(73)
[1]=>
int(49)
}
[4]=>
array(2) {
[0]=>
int(73)
[1]=>
int(51)
}
}
}
Upvotes: 0
Views: 1242
Reputation: 122
foreach ($a as $first_group_key => $first_group) {
foreach ($a as $second_group_key => $second_group) {
if ($second_group_key == $first_group_key) {
continue;
}
$i = count($b[$first_group_key]);
foreach ($second_group as $second_value) {
foreach ($first_group as $first_key => $first_value) {
$b[$first_group_key][$i][0] = $first_value;
$b[$first_group_key][$i][1] = $second_value;
$i++;
}
}
}
}
Upvotes: 1