Create new subfields and add them in specific order

  1. <?php
  2. require 'File/MARC.php';
  3.  
  4. // File_MARC offers the ability to add subfields at any point within
  5. // an existing set of subfields
  6.  
  7. // First, create some subfields
  8. $subfields[] = new File_MARC_Subfield('a', 'nothing');
  9. $subfields[] = new File_MARC_Subfield('z', 'everything');
  10.  
  11. // Then, create a field including those subfields
  12. $field = new File_MARC_Data_Field('100', $subfields, '0');
  13.  
  14. // Create some new subfields
  15. $subfield1 = new File_MARC_Subfield('g', 'a little');
  16. $subfield2 = new File_MARC_Subfield('k', 'a bit more');
  17. $subfield3 = new File_MARC_Subfield('t', 'a lot');
  18.  
  19. // Append a new subfield to the existing set of subfields
  20. // Expected order: a-z-g
  21. $field->appendSubfield($subfield1);
  22.  
  23. // Insert a new subfield after the first subfield with code 'z'
  24. // Expected order: a-z-k-g
  25. $sf = $field->getSubfields('z');
  26. // getSubfields() always returns an array; we just want the first subfield
  27. if (count($sf) > 0) {
  28.   $field->insertSubfield($subfield2, $sf[0]);
  29. }
  30.  
  31. // Insert a new subfield prior to the first subfield with code 'z'
  32. // Expected order: a-t-z-k-g
  33. $sf = $field->getSubfield('z');
  34. // getSubfield() simply returns the first matching subfield
  35. if ($sf) {
  36.   $field->insertSubfield($subfield3, $sf, true);
  37. }
  38.  
  39. // let's see the results
  40. print $field;
  41. print "\n";
  42.  
  43. ?>

Documentation generated on Mon, 01 Jan 2007 23:44:41 -0500 by phpDocumentor 1.3.1