更改单个元素数组的键

时间:2018-12-21 09:52:12

标签: php

我有一个来自数据库的数组树,在这种情况下,我想将子元素的键更改为第二个数组'eric'=> array到整数'0'=> array,如下所示:

0 => Array
    ('text' => 'paris',
     'nodes' => Array
            ('eric' => Array
                    (  'text' => 'eric',
                       'nodes' => Array
                           (0 => Array
                               (
                                'text' => 'so.png',              
                               ),
                           ),
                    ),
            ),
   ),

有我的代码:

while($d = mysqli_fetch_assoc($result)) {
    if(!isset($data[$d['country']])) {
       $data[$d['country']] = array(
         'text' => $d['country'],
         'nodes' => array()  
       ); 
    }
    if(!isset($data[$d['country']]['nodes'][$d['name']])) {
        $data[$d['country']]['nodes'][$d['name']] = array(
          'text' => $d['name'],
          'nodes' => array()
        );    
     }
    array_push($data[$d['country']]['nodes'][$d['name']]['nodes'], $d['n_doc']);
}

2 个答案:

答案 0 :(得分:0)

要将所有子键更改为数字值,只需使用array_values()

  

Live Demo

for($i = 0; $i <= count($data) -1; $i++) { # This loops through each country
    $data[$i]['nodes'] = array_map(function($node) { # This preserves the parent text value
        return array_values($node); # [0] => Paris, [1] => array(...)
    }, $data[$i]['nodes']);
}
  

输出

[ ... => [ text => Paris, nodes => [ 0 => Paris, 1 => [ ... ] ] ... ] ... ]

答案 1 :(得分:0)

您可以更改此输入的代码:

Array
(
    [0] => Array
        (
            [text] => paris
            [nodes] => Array
                (
                    [jacque] => Array
                        (
                            [text] => jacque
                            [nodes] => Array
                                (
                                    [0] => 32.png
                                )
                        )

                    [anis] => Array
                        (
                            [text] => anis
                            [nodes] => Array
                                (
                                    [0] => 5384a97ee9d6b (2).pd
                                )
                        )
                )
        )

    [1] => Array
        (
            [text] => london
            [nodes] => Array
                (
                    [dodo] => Array
                        (
                            [text] => dodo
                            [nodes] => Array
                                (
                                    [0] => 148782.svg
                                    [1] => 333.png
                                )

                        )

                    [sd] => Array
                        (
                            [text] => sd
                            [nodes] => Array
                                (
                                    [0] => 1014-favicon.ico
                                )

                        )

                   )
        )
)