将数组拆分为div

时间:2015-05-04 13:21:59

标签: php arrays loops foreach

我有这个数组:

$result = array('description1', 'description2', 'description3', 'description4', 'description5'

我想将这个数组拆分成这样的div:

  • $ 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>';

            }       

        }

3 个答案:

答案 0 :(得分:2)

这应该适合你:

只需使用array_chunk()对数组进行分块。然后你可以简单地遍历你的数组,将它输出到div和implode()元素。

<?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> 

修改

从更新的数组结构开始,只需先获取所有值:

$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);

答案 1 :(得分:0)

你能不能在div中回应它们:

<div>
    <?php echo $result[0] . "-" . $result[1]; ?>
</div>
<div>
    <?php echo $result[2] . "-" . $result[3]; ?>
</div>
<div>
    <?php echo $result[4]; ?>
</div>

答案 2 :(得分:0)

您只需要控制自己是否处于最后2个位置。

echo '<div>';

for ($i=0;$i<count($result);$i=$i+2) {
    if ($i+1 >= count($result)) {
        echo $result[$i];
    } else {
        echo $result[$i].$result[$i+1];
        echo '</div><div>';
    }
}

echo '</div>;