首页  >  文章  >  后端开发  >  php 如何重置多维数组键名

php 如何重置多维数组键名

WBOY
WBOY原创
2016-06-20 12:33:311705浏览

Array(    [id] => 60179    [pid] => 60071    [name] => 13666520868    [children] => Array        (            [60225] => Array                (                    [id] => 60225                    [pid] => 60179                    [name] => 13587283707                )            [60226] => Array                (                    [id] => 60226                    [pid] => 60179                    [name] => 13511265626                )            [60230] => Array                (                    [id] => 60230                    [pid] => 60179                    [name] => 15088323701                    [children] => Array                        (                            [60234] => Array                                (                                    [id] => 60234                                    [pid] => 60230                                    [name] => 13967187282                                )                        )                )            [60383] => Array                (                    [id] => 60383                    [pid] => 60179                    [name] => 13819296086                    [children] => Array                        (                            [60384] => Array                                (                                    [id] => 60384                                    [pid] => 60383                                    [name] => 18305721297                                )                        )                )        ))



——————————  下面是我想要的结果   ————————————————
因为我要把这数组转成json数据,但是上面的数组中有键名转json无效,要重置从0开始就可以

Array(    [id] => 60179    [pid] => 60071    [name] => 13666520868    [children] => Array        (            [0] => Array                (                    [id] => 60225                    [pid] => 60179                    [name] => 13587283707                )            [1] => Array                (                    [id] => 60226                    [pid] => 60179                    [name] => 13511265626                )            [2] => Array                (                    [id] => 60230                    [pid] => 60179                    [name] => 15088323701                    [children] => Array                        (                            [0] => Array                                (                                    [id] => 60234                                    [pid] => 60230                                    [name] => 13967187282                                )                                                        )                )            [3] => Array                (                    [id] => 60383                    [pid] => 60179                    [name] => 13819296086                    [children] => Array                        (                            [0] => Array                                (                                    [id] => 60384                                    [pid] => 60383                                    [name] => 18305721297                                )                        )                )        ))


回复讨论(解决方案)

$a = array (  'id' => '60179',  'pid' => '60071',  'name' => '13666520868',  'children' =>   array (    60225 =>     array (      'id' => '60225',      'pid' => '60179',      'name' => '13587283707',    ),    60226 =>     array (      'id' => '60226',      'pid' => '60179',      'name' => '13511265626',    ),    60230 =>     array (      'id' => '60230',      'pid' => '60179',      'name' => '15088323701',      'children' =>       array (        60234 =>         array (          'id' => '60234',          'pid' => '60230',          'name' => '13967187282',        ),      ),    ),    60383 =>     array (      'id' => '60383',      'pid' => '60179',      'name' => '13819296086',      'children' =>       array (        60384 =>         array (          'id' => '60384',          'pid' => '60383',          'name' => '18305721297',        ),      ),    ),  ),);function foo(&$ar) {  if(! is_array($ar)) return;  foreach($ar as $k=>&$v) {    if(is_array($v)) foo($v);    if($k == 'children') $v = array_values($v);  }}foo($a);print_r($a);
Array(    [id] => 60179    [pid] => 60071    [name] => 13666520868    [children] => Array        (            [0] => Array                (                    [id] => 60225                    [pid] => 60179                    [name] => 13587283707                )            [1] => Array                (                    [id] => 60226                    [pid] => 60179                    [name] => 13511265626                )            [2] => Array                (                    [id] => 60230                    [pid] => 60179                    [name] => 15088323701                    [children] => Array                        (                            [0] => Array                                (                                    [id] => 60234                                    [pid] => 60230                                    [name] => 13967187282                                )                        )                )            [3] => Array                (                    [id] => 60383                    [pid] => 60179                    [name] => 13819296086                    [children] => Array                        (                            [0] => Array                                (                                    [id] => 60384                                    [pid] => 60383                                    [name] => 18305721297                                )                        )                )        ))

大神谢谢了,太牛了

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn