1

I've got an multidimensional array with some 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 are we?'
    ]

    4 => [
        'label' => 'Logodesign',
        'content' => 'Some content',
        'group' => 'We can offer'
    ]

    5 => [
        'label' => 'Address',
        'content' => 'Some content',
        'group' => 'Who are we?'
    ]

]

The group element is a variety of user input. I want to sort all group elements what are the same into the same array. It's then going to be displayed. If there are only 2 elements with the same group value, then there will only be two columns (50% width on both) in a .row element in HTML, if there are 1 element, only one column (100% width). I'm trying to build a very simple CMS if anyone was wondering why. There may be more easier ways to do this, but I can't think of any.

Any help is appreciated.

EDIT:

I just got the array sorted i think. It looks right.

Now I just need to display it the right way.

$i = 0;
$count = count($data['sections']);
$content = [];

for ($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']];
    }
3
  • And if there are more than two elements? Do you want equal width for all elements so that if there are n elements they get 1/n:th of the space? Commented Jul 14, 2015 at 11:30
  • Yes, exactly. Example the content element will get the class .c-#-12 Commented Jul 14, 2015 at 11:34
  • Then you might want to have a look at this question: stackoverflow.com/questions/6629951/… Commented Jul 14, 2015 at 11:36

1 Answer 1

1

Group the array

Simply loop through the array and put all the elements into a new, nested array:

$content = array();

foreach($data['sections'] as $section) {
   $content[$section['group']][] = $section;
}

This gives you an array $content of this 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 are we?' => [

            ...

    ]

]

Divide the width: Table display

So how do you output this so that every category gets equal width? First you need to loop through the nested array to print some HTML:

<div class="outer">
    <?php foreach($content as $group) { ?>
        <div class="row">
            <?php foreach($group as $item) { ?>
                <div class="item"><?php echo $item['content']; ?></div>
            <?php } ?>
        </div>
    <?php } ?>
</div>

Have a look at this answer for one way to do it using CSS:

div.outer  { display:table; }
div.row    { display:table-row; }
div.item   { display:table-cell; }

Divide the width: Explicit width

Another way to do it is to calculate the width in percentage in PHP and set it explicitly with the width attribute:

<?php foreach($content as $group) { ?>
    <div class="row">
    <?php $w = 100 / count($group); ?>
        <?php foreach($group as $item) { ?>
            <div class="item" width="<?php echo $w; ?>%">
                <?php echo $item['content']; ?>
            </div>
        <?php } ?>
    </div>
<?php } ?>
Sign up to request clarification or add additional context in comments.

1 Comment

That made much more sense than my for loop. I understand it much better now. I have my own small grid system, so I will add classes (if 3 colums, c-4-12). Etc. This helped me alot. Thank you!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.