簡體   English   中英

動態地將新項目深度添加到多維數組

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

我在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
....

我想將此文件解析為JSON對象,以便創建完整的命令樹,然后將其保存到我的MongoDB中。 即:

{
  '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':{...},
       ]

   }
}

實際上,我的腳本正在運行,但我寫了一些我希望改進的靜態邏輯

<?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);
    }
?>

正如你所看到的,當我需要為孩子的孩子等添加一些元素時,我會遇到一些問題。

如何動態地將新子元素添加到其母命令並刪除“switch / case”語句?

謝謝你的提示!

通過引用為當前行設置目標,定位深度數組中的正確位置變得更加容易:

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