php 如何重置多维数组键名

WBOY
發布: 2016-06-20 12:33:31
原創
1704 人瀏覽過

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                                )                        )                )        ))
登入後複製

大神谢谢了,太牛了

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!