简体   繁体   English

动态地将新项目深度添加到多维数组

[英]Dynamically add new items in depth to a multidimensional array

I have a list of commands in a csv file: 我在csv文件中有一个命令列表:

[Parent Full Command ; Command; Command Description]
;show;Show some info
;configure;Configure the equipment
show;conf;display the conf
show;port;display ports informations
show port;interface;Display port interface description
configure;interface;Configure the interface
....

I'd like to parse this file into a JSON object, in order to create the full commands tree and then save it into my MongoDB. 我想将此文件解析为JSON对象,以便创建完整的命令树,然后将其保存到我的MongoDB中。 ie: 即:

{
  'show':{
    'desc': "Display Ports informations",
    'child': [
       'port':{
              'desc': "Display Ports informations",
              'child':[
                       'interface':{
                           'desc':"Display port interface information" },
                       'description':{
                           'desc':"Display port interface description" }
                       ]
       },
       'conf':{...},
       ]

   }
}

Actually, my script is working, but I wrote some static logic I'd like to improve: 实际上,我的脚本正在运行,但我写了一些我希望改进的静态逻辑

<?php
function parsefile($file){
        $fichier_lu = file($file);

        $json = array();
        foreach ($fichier_lu as $numero_ligne => $t) {
            $j = array();

            $T = explode(";",$t);

            $command_m = $T[0];
            $command = $T[1];
            $description = @preg_replace('/\r\n/','',$T[2]);

            if($command_m != "") $com = $command_m." ".$command;
            else $com = $command;

            $j = array(
            'command'=>$com,
            'description' => $description
        );

            $parents = explode(" ",$T[0]);
            $age = sizeof($parents);


            if($age > 1){
                //It sucks down here....
                switch($age){
                    case 2: $json[$parents[0]]['child'][$command] = $j; break;
                    case 3: $json[$parents[0]]['child'][$parents[1]]['child'][$command] = $j; break;
                    case 4: $json[$parents[0]]['child'][$parents[1]]['child'][$parents[2]]['child'][$command] = $j; break;
                    ......
                    ..........
                    ..............
                    default: break;
                }

            } else {
                $json[$command] = $j;
            }
        }
        return json_encode($json);
    }
?>

As you can see, I have some problems when I need to add some elements to the child of a child of a child, etc. 正如你所看到的,当我需要为孩子的孩子等添加一些元素时,我会遇到一些问题。

How can I dynamically add new child elements to their mother command and delete the "switch/case" statement? 如何动态地将新子元素添加到其母命令并删除“switch / case”语句?

Thanks for your tips! 谢谢你的提示!

By setting a target for the current row by reference, targeting the right location in your deep-array becomes much easier: 通过引用为当前行设置目标,定位深度数组中的正确位置变得更加容易:

function parsefile($file,$delimiter=';',$skip_header=1){
   $handle = fopen($file,'r');
   $skip_header = max(0,intval($skip_header));
   while($skip_header > 0){
       fgets($handle);
       $skip_header--;
   }
   $return = array();
   while($data = fgetcsv($handle,0,$delimiter)){
      $command_list = array_filter(explode(' ',$data[0]));
      $target = &$return;
      if(!empty($command_list)){
          foreach($command_list as $command){
             if(!isset($target[$command])) $target[$command] = array();
             if(!isset($target[$command]['child'])) $target[$command]['child'] = array();            
             $target = &$target[$command]['child'];
          }
      }  
      $target[$data[1]] = array('desc' => $data[2]);
      unset($target);
   }
   return json_encode($return);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM