Php 将.dict转换为数组
如何转换.dict文件中的条目,如: 到一个php数组,如 $array['aveu']=array 1=>“确认”,2=>“接纳”Php 将.dict转换为数组,php,arrays,dictionary,Php,Arrays,Dictionary,如何转换.dict文件中的条目,如: 到一个php数组,如 $array['aveu']=array 1=>“确认”,2=>“接纳” 谢谢你的帮助 假设父记录前面没有空格,子记录以逗号分隔,以空格开头,则在文件中的行上循环。如果通过preg_match在前面没有空格,则启动一个新的数组键并分解后续的空格行 $output = array(); $lines = file('yourfile.dict'); foreach ($lines as $line) { // Skip blank l
谢谢你的帮助 假设父记录前面没有空格,子记录以逗号分隔,以空格开头,则在文件中的行上循环。如果通过preg_match在前面没有空格,则启动一个新的数组键并分解后续的空格行
$output = array();
$lines = file('yourfile.dict');
foreach ($lines as $line) {
// Skip blank lines
if (strlen(trim($line)) > 0) {
// No leading whitespace, start a new key:
if (!preg_match('/^\s+/', $line)) {
$key = trim($line);
$output[$key] = array();
}
// Otherwise, explode and add to the previous $key (if $key is non-empty)
else if (!empty($key)) {
$terms = explode(",", $line);
// Trim off whitespace
$terms = array_map('trim', $terms);
// Merge them onto the existing key (if multiple lines)
$output[$key] = array_merge($output[$key], $terms);
}
else {
// Error - no current $key
echo "??? We don't have an active key.";
}
}
}
$output = array();
$lines = file('yourfile.dict');
foreach ($lines as $line) {
// Skip blank lines
if (strlen(trim($line)) > 0) {
// No leading whitespace, start a new key:
if (!preg_match('/^\s+/', $line)) {
$key = trim($line);
$output[$key] = array();
}
// Otherwise, explode and add to the previous $key (if $key is non-empty)
else if (!empty($key)) {
$terms = explode(",", $line);
// Trim off whitespace
$terms = array_map('trim', $terms);
// Merge them onto the existing key (if multiple lines)
$output[$key] = array_merge($output[$key], $terms);
}
else {
// Error - no current $key
echo "??? We don't have an active key.";
}
}
}