duanliaoyu8419 2018-10-03 14:06
浏览 39
已采纳

PHP创建和合并多维数组

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?

  • 写回答

1条回答 默认 最新

  • dpr81047 2018-10-03 14:22
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。