我的指挥档案有:
[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!