简体   繁体   English

PHP从另一个多维数组改造多维数组

[英]php revamp a multi-dimensional array from another multi-dimensional array

I am very new in PHP & would like to revamp below multidiemensional array. 我对PHP非常陌生,想在多维数组下进行改进。 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. 这些数组是我从JSON提交的具有很多“名称”和“值”的FORM中获得的,但是我需要散布一些值集。 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. 提前致谢。

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. 这里没有什么真正复杂的:使用foreach ,您遍历源数组,用键和值(分别从namevalue获取)填充$mapped一个。 And you have another property - $no - for grouping these elements by 4. 而且您还有另一个属性- $no no-将这些元素按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 : 如果不是,则必须在循环中进行其他检查,并从name提取组号:

$mapped = array();
foreach ($formValues as $arr) {
  $name = $arr['name'];
  if (preg_match('/(?<=-)\d+$/', $name, $m)) {
    $mapped[ $m[0] ][$name] = $arr['value'];
  }
}

Try this: 尝试这个:

$out = array_reduce(
    $formValues,
    function (array $carry, array $item) {
        $name = $item['name'];
        $id   = end(explode('-', $name));
        if (! isset($carry[$id])) {
            $carry[$id] = array();
        }
        $carry[$id][$name] = $item['value'];
        return $carry;
    },
    array()
);

print_r($out);

The code groups the input values by the last component of 'name' , using '-' as separator. 代码使用'-'作为分隔符,按'name'的最后一个组成部分对输入值进行分组。 The input values can be in any order, some of them may be missing and there is no request to have all the values for all the groups. 输入值可以按任何顺序排列,其中一些可能会丢失,因此不要求具有所有组的所有值。 For example, if mail-agent-2 is missing the code still works and groups the rest of the values correctly by their IDs. 例如,如果缺少mail-agent-2 ,该代码仍然有效,并通过其ID将其余的值正确分组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM