PHP multi-dimensional array to two-dimensional array

Source: Internet
Author: User
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 ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.