[英]PHP creating and merging multidimensional arrays
I'm having some troubles creating and merging multidimensional arrays. 我在创建和合并多维数组时遇到了一些麻烦。 Trying to explain, I have a dynamic "pack of 4 inputs". 试着解释一下,我有一个动态的“4包输入”。 Those are generated via PHP and the user can add more "4 packs" of inputs. 这些是通过PHP生成的,用户可以添加更多“4包”输入。
Those inputs are named as input1[en]
or input1[pt]
. 这些输入命名为input1[en]
或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: 因此,为了更好地理解input1
的简单$_POST
,它打印出需要分离的foreach的以下数组(仍然是输出2,3和4):
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). 现在,我需要创建一个包含输入中所有信息的数组,但是我在第一个数组创建时遇到了麻烦(来自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]. 如你所见,它错过了[en]中的键[0]。 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])) 所有相同的情况都发生在其他输入上(当同一语言有多个输入时,它只在数组中留下最后一个键(在本例中为[en]))
My others foreach are equal, but changes the 'energy' => array($newvalue => $index2)
to 'attack' => array($newvalue => $index2)
and so on... 我的其他foreach是相同的,但将'energy' => array($newvalue => $index2)
更改为'attack' => array($newvalue => $index2)
等等......
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: 基本上,如果我合并array1
(与array2
生成'energy'
(生成'attack'
),它只保留来自array2
的信息,例如:
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 作为两个数组的示例($ array1和$ array2依次):Array1
Array(
[en] => Array(
[energy] => Array(
[0] => [C][C]
[1] => [L][L][C]
)
)
[pt] => Array(
[energy] => Array(
[0] => [C][C]
)
)
)
Array2 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);
因为我使用$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: 但我只得到$array2
信息的输出:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.