lookingGlass
lookingGlass

Reputation: 365

PHP wrap two render arrays in separate divs

I need to wrap both my arrays in their own wrapping div outside of the foreach loop.

function foo() {
 //my foreach loops

 $top_content[] = $top;
 $bottom_content[] = $bottom;

 return array($top_content, $bottom_content);
}

Ideally I would be able to:

function foo() {
 //my foreach loops

 $top_content[] = '<div class="wrapper-one">' . $top . '</div>';
 $bottom_content[] = '<div class="wrapper-two">' . $bottom  . '</div>';

 return array($top_content, $bottom_content);
}

but then I get an error of: Notice: Array to string conversion

any help appreciated.

Upvotes: 1

Views: 248

Answers (2)

Samundra
Samundra

Reputation: 1909

This is so because you are trying to concatenate array with string. Since $top and $bottom could have been initialized as array before.

$top_content[0] = '<div class="wrapper-one">' . $top . '</div>';
$bottom_content[] = '<div class="wrapper-two">' . $bottom  . '</div>';

So my guess is you must be doing creating $top and $bottom as array and trying to concatenate at the end of for-each loop.

You can still do it like this.

$topContents    = implode('', $top);
$bottomContents = implode('', $bottom);

$top_content[0] = '<div class="wrapper-one">' . $topContents . '</div>';
$bottom_content[] = '<div class="wrapper-two">' . $bottom  . '</div>';

The trick is to use php's implode function to concatenate array first. After concatenation it returns string. This string can then further be concatenated with other strings.

Give it a try and let us know.

Upvotes: 0

Luca Brasi
Luca Brasi

Reputation: 11

function foo() {
 //my foreach loops

 $top_content[0] = '<div class="wrapper-one">' . $top . '</div>';
 $bottom_content[0] = '<div class="wrapper-two">' . $bottom  . '</div>';

 return array($top_content, $bottom_content);
}

Upvotes: 1

Related Questions