【问题标题】:Split array into div [duplicate]将数组拆分为 div [重复]
【发布时间】:2015-05-04 13:21:58
【问题描述】:

我有这个数组:

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

            }       

        }

【问题讨论】:

  • 你试过了吗?
  • 我试试这个$result = array(); foreach(array_chunk($arrayMatches,2) as $k =&gt; $desc) { foreach($desc as $item) { $result[] = $item; } } print_r($result);
  • ^ 将此添加到您的问题中并展示您的努力!
  • 你能用javascript来做吗?
  • 您应该明确您的问题与现有材料有何不同(通过提供对现有材料的参考,然后解释在哪个级别不适合您的情况,效果最好)否则它可能会倾向于太宽泛,不清楚你到底在问什么。

标签: php arrays loops foreach


【解决方案1】:

这应该适合你:

只需使用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);

【讨论】:

  • 它几乎可以工作,但是在第二个和第三个 div 的开头,出现了字符串 'Array'。 &lt;div&gt;Array description3 description4&lt;/div&gt; &lt;div&gt;Array Array description5&lt;/div&gt;
  • @user3653409 那我认为你没有向我们展示你真正的数组结构!请确保向我们展示您的 realfull 数组 + 预期输出的样子
  • @user3653409 请添加 exact 输出:print_r($result);,您会得到。另外,如果您尝试我的代码,请确保您在文件顶部打开了错误报告:&lt;?php ini_set("display_errors", 1); error_reporting(E_ALL); ?&gt; 并告诉我们您是否收到任何提示
  • 确切输出:Array ( [0] =&gt; Array ( [0] =&gt; Array ( [0] =&gt; Array ( [0] =&gt; description1 [1] =&gt; Description2) [1] =&gt; Description3) [1] =&gt; Array ( [0] =&gt; Description4) ) [1] =&gt; Array ( [0] =&gt;Description5) )
  • @user3653409 更新了我的答案。现在它应该可以正常工作了! (您可能还想用真正的数组结构更新您的问题)
【解决方案2】:

你能不能只在 div 中回显它们:

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

【讨论】:

  • 我需要循环执行
  • 对于上述场景,这是想要的输出吗?
【解决方案3】:

你只需要控制你是否在最后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>;

【讨论】:

    猜你喜欢
    • 2021-05-04
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 2017-12-05
    • 1970-01-01
    • 2013-01-03
    相关资源
    最近更新 更多