0

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

3
  • Does this answer your question? PHP - Merging two arrays into one array (also Remove Duplicates) Commented Dec 24, 2020 at 0:51
  • I think that is not merge array, but combine array from thirth element from first array become to be second example above Commented Dec 24, 2020 at 5:08
  • I think I understand -- you want to combine the innermost arrays, regardless of duplicates, and nest with only one level of nesting. Manually with a loop is the best thing I can think of. I would create a function to extract an array value at a certain level of nesting, call that in a loop to build up your result array. Commented Dec 30, 2020 at 15:54

2 Answers 2

1

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"
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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!

3 Comments

Thank you for your solution, but unfortunatelly it's still not work. I have result Array ( [0] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 ) [1] => Array ( [0] => 2 [1] => 2 [2] => 2 [3] => 2 [4] => 2 ) )
Edited, test it again
Thank you for your edited, but there's an error : Notice: A non well formed numeric value encountered

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.