Using array to put multiple classes into a DIV

0

I want to put several classes in a div , using Array , how could I do it? I tried using the code below but it did not work.

<?php 
    $minhas_classes = array(
        'main-content' => 'main-content', 
        'post-count' => 'post-count', 
        'loop-style' => 'loop-style',
        'has-image' => 'has-image',
    );
?>

<div class="<?php echo $minhas_classes; ?>">
</div>
    
asked by anonymous 23.01.2018 / 00:31

1 answer

3

I did not particularly understand why you defined an associative array, where keys are equal to values. In my view, a sequential array would suffice and would be even simpler.

$classes = ['main-content', 'post-count', 'loop-style', 'has-image'];

To display all at once, just convert your array to string with the join

$classes = join(' ', $classes);

So, just do:

echo "<div class=\"{$classes}\">";

See working at Ideone | Repl.it

The result will be, as expected:

<div class="main-content post-count loop-style has-image">
    
23.01.2018 / 00:45