1

I'm having some troubles creating and merging multidimensional arrays. Trying to explain, I have a dynamic "pack of 4 inputs". Those are generated via PHP and the user can add more "4 packs" of inputs.

Those inputs are named as input1[en] or input1[pt]. So, to better understand a simple $_POST of input1, it prints the following array (still out input2,3 and 4) that needs separated foreach:

Array(
[en] => Array(
    [0] => [C][C]
    [1] => [L][L][C]
)
[pt] => Array(
    [0] => [C][C]
)
)

Now, I need to create just one array that contains all the information from inputs, but I'm having some trouble right in the first array creation (from input1).

Using the code:

foreach($_POST['input1'] as $language => $index){
    foreach($index as $newvalue => $index2){
        $cernegy[$language] = array(
            'energy' => array($newvalue => $index2)                 
        );
    }
}

The output is:

Array(
[en] => Array(
    [energy] => Array(
        [1] => [L][L][C]
    )
) [pt] => Array(
    [energy] => Array(
        [0] => [C][C]
    )
)
)

As you can see it misses the key [0] inside [en]. All the same happens with the others inputs (it only left in the array the last key when there are multiple inputs for the same language (in this case [en]))

My others foreach are equal, but changes the 'energy' => array($newvalue => $index2) to 'attack' => array($newvalue => $index2) and so on...

Then, the second problem is merging the arrays. If I merge two arrays (even with wrong information), it only keeps the last merged array. Basically if I merge array1 (that generates 'energy' with array2 (that generates 'attack' ) it only keeps the information from from array2 for example:

Array(
[en] => Array(
    [attack_name] => Array(
        [1] => RazorLeaf
    )
) 
[pt] => Array(
    [attack_name] => Array(
        [0] => Pontapé
    )
)
)

And it should be something like:

Array(
[en] => Array(
    [energy] => Array(
        [1] => [L][L][C]
    )
    [attack_name] => Array(
        [1] => RazorLeaf
    )
) 
[pt] => Array(
    [energy] => Array(
        [0] => [C][C]
    )
    [attack_name] => Array(
        [0] => Pontapé
    )
)
)

I've used this to merge (the array names are example only):

$result = array_merge($array1, $array2);

Could someone point me in the right direction in both problems please? Thanks for your time.

Edit: First part is now solved. The problem remains now in merging two arrays. As an example of two arrays ($array1 and $array2 in order): Array1

Array(
[en] => Array(
    [energy] => Array(
        [0] => [C][C]
        [1] => [L][L][C]
    )
) 
[pt] => Array(
    [energy] => Array(
        [0] => [C][C]
    )
)
)

Array2

Array(
[en] => Array(
    [attack_name] => Array(
        [0] => Tackle
        [1] => RazorLeaf
    )
) 
[pt] => Array(
    [attack_name] => Array(
        [0] => Pontapé
    )
)
)

As I use $result = array_merge($array1, $array2); I was expecting to get:

Array(
[en] => Array(
    [energy] => Array(
        [0] => [C][C]
        [1] => [L][L][C]
    )
    [attack_name] => Array(
        [0] => Tackle
        [1] => RazorLeaf
    )
) 
[pt] => Array(
    [energy] => Array(
        [0] => [C][C]
    )
    [attack_name] => Array(
        [0] => Pontapé
    )
)
)

But I only get as output the $array2 information:

Array(
[en] => Array(
    [attack_name] => Array(
        [0] => Tackle
        [1] => RazorLeaf
    )
) 
[pt] => Array(
    [attack_name] => Array(
        [0] => Pontapé
    )
)
)

What I'm doing wrong?

9
  • 2
    have you tried: $cernegy[$language][] = ['energy' => [$newvalue => $index2]] ? Commented Oct 3, 2018 at 14:12
  • 2
    Or $cernegy[$language]['energy'][] = [$newvalue => $index2] Commented Oct 3, 2018 at 14:19
  • @RiggsFolly your code seems to create the array with all data, but I don't know if it's the best way to create it. Can you check it please? It seems there is more keys than necessary. Array( [en] => Array( [energy] => Array( [0] => Array( [0] => [C][C] ) [1] => Array( [1] => [L][L][C] ) ) ) [pt] => Array( [energy] => Array( [0] => Array( [0] => [C][C] ) ) ) Commented Oct 3, 2018 at 14:34
  • Also the merge still get's the last array only, not merging both. Commented Oct 3, 2018 at 14:35
  • @smoqadam your approach works too, but I prefer the logistic of RiggsFolly Thanks for your time :) Commented Oct 3, 2018 at 14:36

1 Answer 1

3

You are overwriting the value rather than adding to the array. Try something like:

foreach($_POST['input1'] as $language => $index){
    $inner = array();
    foreach($index as $newvalue => $index2){
        $inner[$newvalue] = $index2;
     }
     $cernegy[$language] = array('energy' => $inner);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but it seems it creates new key where it doesnt exists. Por example I have only one key in [pt] and it creates two keys: Array( [en] => Array( [energy] => Array( [0] => [C][C][1] => [L][L][C] ) ) [pt] => Array( [energy] => Array( [0] => [C][C][1] => [L][L][C] ) ) )
@Tiago See the edit, I missed out on wiping out the array once done
thanks. It solved my first problem. I've edited the code with the second problem (arra_merge). Can you check it please?
I've done it. Instead of using array_merge used array_merge_recursive and it worked. Now I have all the information together. Thanks for your time!

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.