将数组拆分为div [英] Split array into div
本文介绍了将数组拆分为div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个数组:
$result = array('description1', 'description2', 'description3', 'description4', 'description5'
我想像这样将这个数组拆分为div:
I want to split this array into divs like this :
- $ result [0]-$ result [1] =>将它们放入div
- $ result [2]-$ result [3] =>将它们放入div
- $ result [4] =>将其放入div
我的整个结构
$content = get_the_content();
$description = array();
$j=0;
if (preg_match_all('/<div id="description" class="description">([^<]*)<\/div>/', $content, $match)) {
for( $i = 0; $i < count($match[0]); $i = $i+1 ) {
$description[] = $match[0][$i];
}
}
$attachments =& get_children($args);
$arrayMatches = array();
if ($attachments) {
foreach(array_chunk($attachments, 2) as $img) {
echo '<div class="two_cols">';
foreach($img as $attachment) {
foreach($attachment as $attachment_key => $attachment_value) {
$imageID = $attachment->ID;
$imageTitle = $attachment->post_title;
$imagearray = wp_get_attachment_image_src($attachment_value, $size, false);
$imageAlt = get_post_meta($imageID, '_wp_attachment_image_alt', true);
$imageURI = $imagearray[0]; // 0 is the URI
$imageWidth = $imagearray[1]; // 1 is the width
$imageHeight = $imagearray[2]; // 2 is the height
?>
<div class="col_1_2">
<!-- A picure Here -->
<?php $arrayMatches[] = $match[0][$j]; ?>
</div>
<?php
break;
}
$j++;
}
$arrayMatches = array_chunk($arrayMatches, 2);
echo "<div>";
foreach($arrayMatches as $v) {
echo implode($v);
}
echo "</div>";
echo '</div>';
}
}
推荐答案
这应该对您有用:
只需使用 array_chunk()
对您的数组进行分块.然后,您可以简单地遍历数组,将其输出到div和 implode()
元素.
Just chunk your array with array_chunk()
. And then you can simply loop through your array, output it into a div and implode()
the elements.
<?php
$result = array('description1', 'description2', 'description3', 'description4', 'description5');
$result = array_chunk($result, 2);
foreach($result as $v) {
echo "<div>" . implode(" ", $v) . "</div>";
}
?>
输出:
<div>description1 description2</div>
<div>description3 description4</div>
<div>description5</div>
从更新后的数组结构中开始,首先要像这样首先获取所有值:
As from your updated array structure just grab all values first like this:
$result = [];
$arr = array(['description1'], ['description2'], 'description3', 'description4', 'description5'); //example
array_walk_recursive($arr, function($v, $k)use(&$result){
$result[] = $v;
});
$result = array_chunk($result, 2);
这篇关于将数组拆分为div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文