我有此數組在PHP返回從數據庫PHP數組。必須有一個更簡單的做法這
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幾乎是不可讀的。必須有一個更簡單的方法
提供var_export()樣本陣列這樣的脂肪酶可以測試 – goat 2011-01-08 03:31:26
它不必是一定是締合輸出,導線,Copperwires ...可以通過NUMERICS取代的(事實上,我寧願) – 2011-01-08 03:32:28