douji5329 2014-12-13 14:01
浏览 51
已采纳

php从另一个多维数组中修改一个多维数组

I am very new in PHP & would like to revamp below multidiemensional array. These array i am getting from JSON submit of a FORM with lots of 'name' & 'value' but i need to spread out few set of values. Here i only given a example that is similar of my work.

$formValues = array(
    array(
        'name' => 'name-agent-1',
        'value' => 'austin'
    ),
    array(
        'name' => 'company-agent-1',
        'value' => 'Samsung'
    ),
    array(
        'name' => 'phone-agent-1',
        'value' => '889410'
    ),
    array(
        'name' => 'mail-agent-1',
        'value' => 'abc@mail.com'
    ),
    array(
        'name' => 'name-agent-2',
        'value' => 'Justin'
    ),
    array(
        'name' => 'company-agent-2',
        'value' => 'Nokia'
    ),
    array(
        'name' => 'phone-agent-2',
        'value' => '332100'
    ),
    array(
        'name' => 'mail-agent-2',
        'value' => 'xyz@mail.com'
    ),
    array(
        'name' => 'name-agent-3',
        'value' => 'stefen'
    ),
    array(
        'name' => 'company-agent-3',
        'value' => 'Motorolla'
    ),
    array(
        'name' => 'phone-agent-3',
        'value' => '8744520'
    ),
    array(
        'name' => 'mail-agent-3',
        'value' => 'tyu@mail.com'
    )
);

From the above example of Multi-dimensional array i would like to re-build on below format:

Array
  (
   [1] => Array
       (
        [name-agent-1] => austin
        [company-agent-1] => Samsung
        [phone-agent-1] => 889410
        [mail-agent-1] => abc@mail.com
    )
  [2] => Array
    (
        [name-agent-2] => Justin
        [company-agent-2] => Nokia
        [phone-agent-2] => 332100
        [mail-agent-2] => xyz@mail.com
    )
  [3] => Array
    (
        [name-agent-3] => stefen
        [company-agent-3] => Motorolla
        [phone-agent-3] => 8744520
        [mail-agent-3] => tyu@mail.com
    )
)

I need your the shortest & easiest way to make this happen. Please help and appreciated the suggestion that makes sense. Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dongmacheng3222 2014-12-13 14:07
    关注

    One possible approach:

    $mapped = array();
    $group = 0; // assuming you indeed have to start indexing with 1, as in the question
    foreach ($formValues as $i => $arr) {
      if ($i % 4 === 0) {
        $group++;
      }
      $mapped[$group][ $arr['name'] ] = $arr['value'];
    }
    

    Demo. There's nothing really complicated here: with foreach, you iterate over the source array, filling out $mapped one with keys and values, taken from name and value correspondingly. And you have another property - $no - for grouping these elements by 4.


    In the code above I've assumed the source array always has the same structure. If it's not, you'll have to do additional checks within the loop, extracting the group number off the name:

    $mapped = array();
    foreach ($formValues as $arr) {
      $name = $arr['name'];
      if (preg_match('/(?<=-)\d+$/', $name, $m)) {
        $mapped[ $m[0] ][$name] = $arr['value'];
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?