English 中文(简体)
积极增加一个多层面阵容的新项目
原标题:Dynamically add new items in depth to a multidimensional array

我的指挥档案有:

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

   }
}

实际上,我的文字是行之有效的,但我放弃一些静态逻辑。 我愿改进:

<?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( /
/ ,  ,$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);
    }
?>

你们可以看到,在我需要为一名儿童的子女增加一些内容时,我有一些问题。

我怎么能积极地在母亲指挥中增加新的儿童内容,删除“巫术/个案”声明?

Thanks for your tips!

最佳回答

通过参照确定目前增长的目标,把你深层的正确位置作为目标更容易:

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);
}
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签