Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7064685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:51:52+00:00 2026-05-28T04:51:52+00:00

I have a list of commands in a csv file: [Parent Full Command ;

  • 0

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.
i.e:

{
  '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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:51:53+00:00Added an answer on May 28, 2026 at 4:51 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file containing a list of unix commands like ls -la /etc/passwd.
I have a csv file and when i use this command SOLK<-read.table('Book1.csv',header=TRUE,sep=';') I get
I have a batch file that outputs a list of commands which themselves can
I have some code like this: case class FunctionCommand[A](function: Function1[Array[A], Unit]) class MyClass(commands: List[FunctionCommand[_]])
I have a list/queue of 200 commands that I need to run in a
I have read the other posts, e.g., vim: Executing a list of editor commands
I have list of catalog paths and need to filter out some of them.
I have a huge CSV file I would like to process using Hadoop MapReduce
I have a command that formats it's output in the form of CSV. I
I have a list of http commands which need to be executed in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.