简体   繁体   中英

Dynamically add new items in depth to a multidimensional array

I have a list of commands in a csv file:

[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. 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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