-2

次のような.dictファイルからエントリを変換するにはどうすればよいですか:

aveu
    acknowledgement, admission

のようなphp配列に

$array['aveu'] = array( 1 => '承認', 2 => '入場');

助けてくれてありがとう!

4

1 に答える 1

0

親の前に空白がなく、子レコードが空白で始まるカンマで区切られていると仮定すると、ファイル内の行をループします。( 経由でpreg_match()) 先頭に空白がない場合は、新しい配列キーとexplode()後続の空白行を開始します。

$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.";
    }
  }
}
于 2012-07-26T02:42:29.427 に答える