[英]PHP Get all alike values from array
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. group
元素是各种用户输入。 I want to sort all group
elements what are the same into the same array. 我想将所有相同的
group
元素排序到同一数组中。 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). 如果只有2个元素具有相同的
group
值,那么HTML的.row
元素中将只有两列(两个元素的宽度均为50%),如果只有1个元素,则只有一列(宽度为100%)。 I'm trying to build a very simple CMS if anyone was wondering why. 如果有人想知道为什么,我正在尝试构建一个非常简单的CMS。 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']];
}
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: 这将为您提供此格式的
$content
数组:
[
'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?' => [
...
]
]
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:
首先,您需要遍历嵌套数组以打印一些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: 查看使用CSS的一种方法的答案 :
div.outer { display:table; }
div.row { display:table-row; }
div.item { display:table-cell; }
Another way to do it is to calculate the width in percentage in PHP and set it explicitly with the width
attribute: 另一种方法是用PHP计算百分比宽度,并使用
width
属性显式设置它:
<?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 } ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.