Reputation: 29
My array data :
$opt_val = Array ( [0] => Array ( [0] => Array ( [0] => 0|0|0|P3D [1] => 0|0|1|P4D [2] => 0|0|2|P5D ) [1] => Array ( [0] => 0|1|0|P3D [1] => 0|1|1|P4D [2] => 0|1|2|P5D ) ) [1] => Array ( [0] => Array ( [0] => 1|0|0|P3D [1] => 1|0|1|P4D [2] => 1|0|2|P5D ) [1] => Array ( [0] => 1|1|0|P3D [1] => 1|1|1|P4D [2] => 1|1|2|P5D ) ) )
I want to join above array with result :
Array ( [0] => Array ( [0] => 0|0|0|P3D#0|1|0|P3D (from Array[0][0][0]#Array[0][1][0]) [1] => 0|0|1|P4D#0|1|1|P4D [2] => 0|0|2|P5D#0|1|2|P5D ) [1] => Array ( [0] => 1|0|0|P3D#1|1|0|P3D (from Array[1][0][0]#Array[1][1][0]) [1] => 1|0|1|P4D#1|1|1|P4D [2] => 1|0|2|P5D#1|1|2|P5D ) )
My code
for ($ov = 0; $ov < count($opt_val); $ov++) {
for ($ovi = 0; $ovi < count($opt_val[$ov]); $ovi++) {
for ($iv = 0; $iv < count($opt_val[$ov][$iv]); $iv++) {
$im_opt_val[$iv] = implode("#", $opt_val[$ov][$ovi]);
}
$impl_opt_val[$ov] = $im_opt_val;
}
}
Thank you
Upvotes: 0
Views: 54
Reputation: 4355
The following code snippet should do the trick.
<?php
declare(strict_types=1);
$opt_val = [
[
[
'0|0|0|P3D',
'0|0|1|P4D',
'0|0|2|P5D',
],
[
'0|1|0|P3D',
'0|1|1|P4D',
'0|1|2|P5D',
],
],
[
[
'1|0|0|P3D',
'1|0|1|P4D',
'1|0|2|P5D',
],
[
'1|1|0|P3D',
'1|1|1|P4D',
'1|1|2|P5D',
],
],
];
$result = [];
foreach ($opt_val as $outerArrayKey => $outerArray) {
foreach ($outerArray as $innerArray) {
foreach ($innerArray as $innerArrayKey => $innerArrayElement) {
if (!isset($result[$outerArrayKey][$innerArrayKey])) {
$result[$outerArrayKey][$innerArrayKey] = $innerArrayElement;
} else {
$result[$outerArrayKey][$innerArrayKey] .= '#'.$innerArrayElement;
}
}
}
}
var_dump($result);
The output would be:
array(2) {
[0]=>
array(3) {
[0]=>
string(19) "0|0|0|P3D#0|1|0|P3D"
[1]=>
string(19) "0|0|1|P4D#0|1|1|P4D"
[2]=>
string(19) "0|0|2|P5D#0|1|2|P5D"
}
[1]=>
array(3) {
[0]=>
string(19) "1|0|0|P3D#1|1|0|P3D"
[1]=>
string(19) "1|0|1|P4D#1|1|1|P4D"
[2]=>
string(19) "1|0|2|P5D#1|1|2|P5D"
}
}
Upvotes: 1
Reputation: 579
I hope this work for you:
$finalFinalArray = [];
foreach($outerArray as $key => $innerArray){
$finalArray = [];
$inQueueArray = [];
foreach ($innerArray as $inInnerArray) {
$inQueueArray = array_merge($inQueueArray, $inInnerArray);
}
// Now, inner array is merged
for ($i = 0; $i < count($inQueueArray); $i++){
$finalArray[] = $inQueueArray[$i] + "#" + $inQueueArray[$i+3];
}
$finalFinalArray[] = $finalArray;
}
var_dump($finalFinalArray);
Didn't test it!
Upvotes: 0