php 如何重置多维数组键名

WBOY
Libérer: 2016-06-20 12:33:31
original
1748 Les gens l'ont consulté

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                                )                        )                )        ))
Copier après la connexion



—————————— 下面是我想要的结果 ————————————————
因为我要把这数组转成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                                )                        )                )        ))
Copier après la connexion


回复讨论(解决方案)

$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);
Copier après la connexion
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                                )                        )                )        ))
Copier après la connexion

大神谢谢了,太牛了

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal