我用换行符分隔文本
/EXT/123/SOMEPARAM:VALUE123
/EXT/123/other:VALUE222
我创建了一些代码来创建带有孩子的数组。
array_merge_recursive 如果键是数字,则数组不正确,我添加 @ 符号,结果是正确的
$astdb_array=explode("\n", $astdb_raw);
$db_data = []; $result=[];
foreach($astdb_array as $db_row){
$db_row = trim(preg_replace('/\s/', '', $db_row));
$row_elements = explode('/', $db_row);
if(sizeof($row_elements)>0){
$key = array_shift($row_elements);
#$db_data= create_array($row_elements);
foreach(array_reverse($row_elements) as $child){
if (strpos($child, ':') !== false) {
[$kkey, $vval] = explode(':',$child);
$key = [$kkey => $key = $vval];
}else{
if(is_numeric($child)){
$key = [ '@'.$child => $key];
}else{
$key = [$child => $key];
}
}
}
}
}
$array_result = call_user_func_array('array_merge_recursive',$db_data);
print json_encode($array_result);
函数结果
Array
(
[EXT] => Array
(
[@104] => Array
(
[MOBILE] => 77761587000
[TENANT] => customer1
[TRUNK] => customer1
[TRUNKID] => 2
)
[@110] => Array
(
[MOBILE] => 77761587000
[TENANT] => customer1
[TENANTID] => 2
[TRUNK] => customer1
[TRUNKID] => 2
)
)
[ROUTES] => Array
(
[@1] => Array
(
[PATTERN] => 5555
[PERMS] => type,any,end
)
[@2] => Array
(
[NAME] => KZ
[PATTERN] => _XXXXXXXXXXX
[PERMS] => type,any,end
[TYPE] => OUTDOOR
)
)
)
结果正确,但需要从键中删除符号@,尝试使用:
$result = array_map(function($s){return str_replace('^','',$s);}, $array_result);
这对我没有帮助。请帮忙解决这个问题.... 谢谢