使用 foreach() 遍历文件;需要在打印前删除重复的结果

Looping through files with foreach(); need to remove duplicate results before printing

我正在编写一些代码来实现自动化 fontfaceobserver.js。对于目录中的每个字体文件,我需要获取字体系列名称,以便我可以在 javascript 中打印它。一些字体共享相同的系列名称,后跟样式名称,例如 cousine-webfont.woff & cousine-italic-webfont.woff。我只想打印这些重复项的第一次出现并跳过其余部分。

我试过使用 array_unique() 但我想我做错了。

function my_fontload() {
  // Locate font files
  $font_path = get_stylesheet_directory_uri() . "/path/to/fonts/";
  $files = glob(get_stylesheet_directory( __FILE__ ) . '/path/to/fonts/*.woff', GLOB_BRACE);

  $suffix = '-webfont';

  $observer = A;

  foreach($files as &$file) {

    $obs = $observer++;

    $font = basename($file, '.woff'); // remove the file type
    $font = str_replace($suffix, '', $font); // remove the -webfont suffix
    $family = explode("-", $font);

    // Various attempts:

    // $fam = $family[0]; // First needle. Creates duplicates if present.
    // $fam = array_unique($family[0]); // Doesn't work. Outputs nothing.
    // $fam = array_unique($fam); // Doesn't work. Outputs nothing.
    // $fam = array_unique(array($fam)); // Outputs string 'Array'.

    echo '

    var font'. $obs . '=new FontFaceObserver( \\'' . $fam. '\\' );

    ';
  }

  unset ($observer);
  unset ($file);

}

期望输出:

var fontA=new FontFaceObserver( \'cousine\' );
var fontB=new FontFaceObserver( \'liberationmono\' );
var fontC=new FontFaceObserver( \'merriweather\' );
var fontD=new FontFaceObserver( \'merriweathersans\' );

输出使用 $fam = $family[0];

var fontA=new FontFaceObserver( \'cousine\' );
var fontB=new FontFaceObserver( \'cousine\' );
var fontC=new FontFaceObserver( \'liberationmono\' );
var fontD=new FontFaceObserver( \'liberationmono\' );
var fontE=new FontFaceObserver( \'liberationmono\' );
var fontF=new FontFaceObserver( \'merriweather\' );
var fontG=new FontFaceObserver( \'merriweather\' );
var fontH=new FontFaceObserver( \'merriweather\' );
var fontI=new FontFaceObserver( \'merriweather\' );
var fontJ=new FontFaceObserver( \'merriweathersans\' );
var fontK=new FontFaceObserver( \'merriweathersans\' );

如果使用 $fam = array_unique($family[0]); $fam = $family[0]; 结合 $fam = array_unique($fam);

var fontA=new FontFaceObserver( \'\' );
var fontB=new FontFaceObserver( \'\' );
var fontC=new FontFaceObserver( \'\' );
var fontD=new FontFaceObserver( \'\' );
var fontE=new FontFaceObserver( \'\' );
var fontF=new FontFaceObserver( \'\' );
var fontG=new FontFaceObserver( \'\' );
var fontH=new FontFaceObserver( \'\' );
var fontI=new FontFaceObserver( \'\' );
var fontJ=new FontFaceObserver( \'\' );
var fontK=new FontFaceObserver( \'\' );

如果使用 $fam = $family[0]; 结合 $fam = array_unique(array($fam));

var fontB=new FontFaceObserver( \'Array\' );
var fontC=new FontFaceObserver( \'Array\' );
var fontD=new FontFaceObserver( \'Array\' );
var fontE=new FontFaceObserver( \'Array\' );
var fontF=new FontFaceObserver( \'Array\' );
var fontG=new FontFaceObserver( \'Array\' );
var fontH=new FontFaceObserver( \'Array\' );
var fontI=new FontFaceObserver( \'Array\' );
var fontJ=new FontFaceObserver( \'Array\' );
var fontK=new FontFaceObserver( \'Array\' );

我也试过在主数组中使用第二个 foreach() 来生成一个临时数组,我可以从中提取唯一结果,但我没有成功。

所以我想出了如何做到这一点。绝对需要将 foreach() 的结果存储在另一个循环中,然后提取唯一的结果。

function my_fontloadtest() {
  // Locate font files
  $font_path = get_stylesheet_directory_uri() . "/path/to/fonts/";
  $files = glob(get_stylesheet_directory( __FILE__ ) . '/path/to/fonts/*.woff', GLOB_BRACE);

  $suffix = '-webfont';

  $observer = A;

  $fam = array(); // declare this outside the loop or it will be overwritten after each loop

  foreach ($files as &$file) {

    $font = basename($file, '.woff'); // remove the file type
    $font = str_replace($suffix, '', $font); // remove the -webfont suffix
    $family = explode("-", $font);
    $fam[] = $family[0]; // First needle. Square braces needed to add the items to the array

  }

  $results = array_unique($fam); // Now that we've created the array, filter for duplicates
  $result = array($results[0]); // Make an array from the filtered results
  foreach ($results as &$result) {
    $obs = $observer++;
    echo 'var font'. $obs . '=new FontFaceObserver( \\'' . $result. '\\' );';
    echo "\r\n";
  }


  unset ($observer);
  unset ($file);

}