html - PHP Get all alike values from array -
i've got multidimensional array values.
[ 1 => [ 'label' => 'seo', 'content' => 'some content', 'group' => 'we can offer' ] 2 => [ 'label' => 'webdesign', 'content' => 'some content', 'group' => 'we can offer' ] 3 => [ 'label' => 'contact', 'content' => 'some content', 'group' => 'who we?' ] 4 => [ 'label' => 'logodesign', 'content' => 'some content', 'group' => 'we can offer' ] 5 => [ 'label' => 'address', 'content' => 'some content', 'group' => 'who we?' ] ]
the group
element variety of user input. want sort group
elements same same array. it's going displayed. if there 2 elements same group
value, there 2 columns (50% width on both) in .row
element in html, if there 1 element, 1 column (100% width). i'm trying build simple cms if wondering why. there may more easier ways this, can't think of any.
any appreciated.
edit:
i got array sorted think. looks right.
now need display right way.
$i = 0; $count = count($data['sections']); $content = []; ($i = 0; $i < $count; $i++) { if (!in_array($data['sections'][$i]['group'], $content)) { $content[] = $data['sections'][$i]['group']; } $content[$data['sections'][$i]['group']][] = ['label' => $data['sections'][$i]['label'], 'content' => $data['sections'][$i]['content']]; }
group array
simply loop through array , put elements new, nested array:
$content = array(); foreach($data['sections'] $section) { $content[$section['group']][] = $section; }
this gives array $content
of format:
[ 'we can offer' => [ 1 => [ 'label' => 'seo', 'content' => 'some content', 'group' => 'we can offer' ] 2 => [ 'label' => 'webdesign', 'content' => 'some content', 'group' => 'we can offer' ] ... ] 'who we?' => [ ... ] ]
divide width: table display
so how output every category gets equal width? first need loop through nested array print html:
<div class="outer"> <?php foreach($content $group) { ?> <div class="row"> <?php foreach($group $item) { ?> <div class="item"><?php echo $item['content']; ?></div> <?php } ?> </div> <?php } ?> </div>
have @ answer 1 way using css:
div.outer { display:table; } div.row { display:table-row; } div.item { display:table-cell; }
divide width: explicit width
another way calculate width in percentage in php , set explicitly width
attribute:
<?php foreach($content $group) { ?> <div class="row"> <?php $w = 100 / count($group); ?> <?php foreach($group $item) { ?> <div class="item" width="<?php echo $w; ?>%"> <?php echo $item['content']; ?> </div> <?php } ?> </div> <?php } ?>
Comments
Post a Comment