PHP数组。必须有一个更简单的方法来做到这一点

时间:2011-01-08 03:16:34

标签: php arrays

我在db中返回了这个数组

Array
(
    [inv_templates] => Array
        (
            [0] => Array
                (
                    [inven_subgroup_template_id] => 1
                    [inven_group] => Wires
                    [inven_subgroup] => CopperWires
                    [inven_template_id] => 1
                    [inven_template_name] => CopperWires6G
                    [constrained] => 0
                    [value_constraints] => 
                    [accept_range] => 2 - 16
                    [information] => Measured Manual
                )

            [1] => Array
                (
                    [inven_subgroup_template_id] => 1
                    [inven_group] => Wires
                    [inven_subgroup] => CopperWires
                    [inven_template_id] => 2
                    [inven_template_name] => CopperWires2G
                    [constrained] => 0
                    [value_constraints] => 
                    [accept_range] => 1 - 7
                    [information] => Measured by Automated Calipers
                )

        )

)

我需要输出这种多维的东西

Array
(
    [Wires] => Array
        (
            [inv_group_name] => Wires
            [inv_subgroups] => Array
                (
                    [CopperWires] => Array
                        (
                            [inv_subgroup_id] => 1
                            [inv_subgroup_name] => CopperWires
                            [inv_templates] => Array
                                (
                                    [CopperWires6G] => Array
                                        (
                                            [inv_name] => CopperWires6G
                                            [inv_id] => 1
                                        )

                                    [CopperWires2G] => Array
                                        (
                                            [inv_name] => CopperWires2G
                                            [inv_id] => 2
                                        )

                                )

                        )

                )

        )

)


我目前正在做这些事情

foreach ($data['inv_templates'] as $key => $value) {
        $processeddata[$value['inven_group']]['inv_group_name'] = $value['inven_group'];
        $processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_subgroup_id'] = $value['inven_subgroup_template_id'];
        $processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_subgroup_name'] = $value['inven_subgroup'];
        $processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_templates'][$value['inven_template_name']]['inv_name'] =  $value['inven_template_name'];
        $processeddata[$value['inven_group']]['inv_subgroups'][$value['inven_subgroup']]['inv_templates'][$value['inven_template_name']]['inv_id'] =  $value['inven_template_id'];

    }
    return $processeddata;


编辑:一个var_export

array (
  'inv_templates' => 
  array (
    0 => 
    array (
      'inven_subgroup_template_id' => '1',
      'inven_group' => 'Wires',
      'inven_subgroup' => 'CopperWires',
      'inven_template_id' => '1',
      'inven_template_name' => 'CopperWires6G',
      'constrained' => '0',
      'value_constraints' => '',
      'accept_range' => '2 - 16',
      'information' => 'Measured Manual',
    ),
    1 => 
    array (
      'inven_subgroup_template_id' => '1',
      'inven_group' => 'Wires',
      'inven_subgroup' => 'CopperWires',
      'inven_template_id' => '2',
      'inven_template_name' => 'CopperWires6G',
      'constrained' => '0',
      'value_constraints' => '',
      'accept_range' => '1 - 7',
      'information' => 'Measured by Automated Calipers',
    ),
  ),
)

foreach几乎不可读。必须有一个更简单的方法

3 个答案:

答案 0 :(得分:1)

$processeddata = array();

foreach($data['inv_templates'] as $key => $value) { 
    $group = $value['inven_group'];
    $processeddata[$group]['inv_group_name'] = $group;

    $subgroup = &$processeddata[$group]['inv_subgroups'][$value['inven_subgroup']];
    $subgroup['inv_subgroup_id'] = $value['inven_subgroup_template_id'];
    $subgroup['inv_subgroup_name'] = $value['inven_subgroup'];

    $template = $value['inven_template_name'];
    $subgroup['inv_templates'][$template]['inv_name'] = $template;
    $subgroup['inv_templates'][$template]['inv_id'] = $value['inven_template_id'];
}

return $processeddata;

答案 1 :(得分:0)

未经测试的代码。这以多维方式构造数组,然后使用array_merge_recursive将它们与已处理的数据合并。

if (!isset($processeddata[$value['inven_group']]))
    $processeddata[$value['inven_group']] = array();

$processeddata[$value['inven_group']] = array_merge_recursive(
    $processeddata[$value['inven_group']],
    array(
        'inv_group_name' => $value['inven_group'],
        'inv_subgroups' => array(
            $value['inven_subgroup'] => array(
                'inv_subgroup_id' => $value['inven_subgroup_template_id'],
                'inv_subgroup_name' => $value['inven_subgroup'],
                'inv_templates' => array(
                    $value['inven_template_name'] => array(
                        'inv_name' => $value['inven_template_name'],
                        'inv_id' => $value['inven_template_id'],
                    ),
                ),
            ),
        ),
    )
);

答案 2 :(得分:0)

我发现这种格式通常对我有用。你可以做得更有效率,我从来不关心:D

虽然我开始遍历$ yourArray ['inv_templates']。

function groupToStructure(array $rows, array $keys) {
    $tree = array();
    $keys = array_reverse($keys);
    foreach ($rows as $row) {
        $subTree = array($row);
        foreach ($keys as $key) {
            $subTree = array($row[$key] => $subTree);
        }
        $tree = array_merge_recursive($tree, $subTree);
    }
    return $tree;
}

print_r(groupToStructure($rows, array('inven_group', 'inven_subgroup', 'inven_template_name')));