php-marc icon indicating copy to clipboard operation
php-marc copied to clipboard

How to loop through all sub fields of a field

Open hackerspk opened this issue 2 years ago • 1 comments

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") {
...
}

}

hackerspk avatar Jan 17 '23 03:01 hackerspk

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

danmichaelo avatar Feb 27 '24 23:02 danmichaelo