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?
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.