At present, there is a multi-dimensional array with irregular hierarchies. You need to convert it into a two-dimensional array. The source data is as follows. {Code...} the array structure in the preceding format has many layers and looks messy. If you use chrome with the phpview plug-in, the structure is like this. {Code ....
At present, there is a multi-dimensional array with irregular hierarchies. You need to convert it into a two-dimensional array.
The source data is as follows.
$a = Array('0' => Array( 'id' => '125','level' => '0','child' => Array( '0' => Array('id' => '189' ,'level' => '1','child' => Array( '0' => Array( 'id' => '425', 'level' => '2','child' => Array( '0' => Array( 'id' => '385', 'level' => '3' ), '1' => Array( 'id' => '782', 'level' => '3' ), ), ), ), ), '1' => Array('id' => '688','level' => '1'),),),);
The array structure in the preceding format has many layers and looks messy. If you use chrome with the phpview plug-in, the structure is like this.
Array('0' => Array('id' => '125','level' => '0','parent_id'='0'),'1' => Array('id' => '189','level' => '1','parent_id'='125'),'2' => Array('id' => '425','level' => '2','parent_id'='189'),'3' => Array('id' => '385','level' => '3','parent_id'='425'), '4' => Array('id' => '782','level' => '3','parent_id'='425'), '5' => Array('id' => '688','level' => '1','parent_id'='125'));
Here, each parent_id is the id of the Upper-layer array, and the first-layer parent_id is 0.
Reply content:
At present, there is a multi-dimensional array with irregular hierarchies. You need to convert it into a two-dimensional array.
The source data is as follows.
$a = Array('0' => Array( 'id' => '125','level' => '0','child' => Array( '0' => Array('id' => '189' ,'level' => '1','child' => Array( '0' => Array( 'id' => '425', 'level' => '2','child' => Array( '0' => Array( 'id' => '385', 'level' => '3' ), '1' => Array( 'id' => '782', 'level' => '3' ), ), ), ), ), '1' => Array('id' => '688','level' => '1'),),),);
The array structure in the preceding format has many layers and looks messy. If you use chrome with the phpview plug-in, the structure is like this.
Array('0' => Array('id' => '125','level' => '0','parent_id'='0'),'1' => Array('id' => '189','level' => '1','parent_id'='125'),'2' => Array('id' => '425','level' => '2','parent_id'='189'),'3' => Array('id' => '385','level' => '3','parent_id'='425'), '4' => Array('id' => '782','level' => '3','parent_id'='425'), '5' => Array('id' => '688','level' => '1','parent_id'='125'));
Here, each parent_id is the id of the Upper-layer array, and the first-layer parent_id is 0.
Do a recursive processing.
function reformat($arrTmp, $parent_id=0, &$ret=null) { foreach($arrTmp as $k => $v) { $ret[$v['id']] = array('id'=>$v['id'], 'level'=>$v['level'], 'parent_id'=>$parent_id); if($v['child']) { reformat($v['child'], $v['id'], $ret); } } return $ret;}var_dump(reformat($a));
You're welcome.
Class Test {public function performance_arr ($ arr) {static $ temp = array (); // declares a static local variable foreach ($ arr as $ key => $ val) {if (is_array ($ val) {$ this-> performance_arr ($ val);} else {$ temp [$ key] = $ val ;}} return $ temp ;}}