欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 如何重置多维数组键名

程序员文章站 2022-06-09 09:53:34
...
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                                )                        )                )        ))

大神谢谢了,太牛了