I'm creating a function called getAttributeTitle() which basically as the name suggests, gets the name of the title. I have hit a wall however and tried searching many things but couldn't really find anything - my goal is to return the value in a template file $this->getAttributeTitle but the foreach obviously would only return the last value in the foreach - not dynamic

here's my code:

public function getAttributeTitle() {
    $attributeTitle = $this->getAttributes();
    foreach ($attributeTitle as $attrTitle) {
        $title = str_replace('_', ' ', $attrTitle);

    return $attributeTitle;

all I want is foreach of the attribute titles - return the title in a user friendly way but not sure how - any ideas?


1 Answer 1


You're pretty close. You have a couple options: return an Array or return a string from an array.

Here's how I'd return a string of all titles separated by a space:

public function getAttributeTitle() {
    $titles = array();
    foreach ($this->getAttributes() as $title) {
        $titles[] = str_replace('_', ' ', $title);

    return implode(' ', $titles);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.