phparraysforeachphp-7.3array-unique

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


I'm writing some code to automate fontfaceobserver.js. For each font file in a directory I need to get the font family name so I can print it in the javascript. Some of the fonts share the same family name followed by a style name, e.g, cousine-webfont.woff & cousine-italic-webfont.woff. I want to print only the first occurrence of these duplicates and skip the rest.

I've tried using array_unique() but I guess I'm doing it wrong.

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);

}

Desired output:

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

Output using $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\' );

If using $fam = array_unique($family[0]); or $fam = $family[0]; combined with $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( \'\' );

If using $fam = $family[0]; combined with $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\' );

I've also tried using a second foreach() inside the main one to generate a temporary array that I could extract the unique results from but I was unsuccessful.


Solution

  • So I figured out how to do this. Definitely needed to store the results of the foreach() inside another loop, then extract the unique results.

    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);
    
    }