scriptotek / php-marc

Simple interface for working with MARC records using the File_MARC package
MIT License
51 stars 11 forks source link

How to loop through all sub fields of a field #24

Open hackerspk opened 1 year ago

hackerspk commented 1 year ago

Thanks for the great code.

How can I loop through all subfields of a field. For example the field 505 is generally used to store table of contents of a book. It has a,t,g,r sub fields. Is it possible to loop through all sub fields of 505 and check sub field name. something like:

foreach ($field as $subfield) { 

if ($subfield == "g") {
...
}

}
danmichaelo commented 7 months ago

Here's an example:

foreach ($record->getFields("505") as $field) {
    foreach ($field->getSubfields() as $subfield) {
        if ($subfield->getCode() === "t") {
            ...
        }
    }
}

A general tip is to autocomplete "get" to get an overview of available methods:

image