通过爆炸阵列循环

时间:2016-08-22 14:31:00

标签: php arrays loops

我想循环一个爆炸的数组,但我仍然坚持如何去做。

我的代码:

+-----------+-----------+------+-----+-------------------+-----------------------------+
| Field     | Type      | Null | Key | Default           | Extra                       |
+-----------+-----------+------+-----+-------------------+-----------------------------+
| Key1      | int(10)   | NO   | PRI |                   |                             |
| Key2      | int(10)   | NO   | PRI |                   |                             |
| DateAdded | DATETIME  | NO   |     |                   |                             |
+-----------+-----------+------+-----+-------------------+-----------------------------+

我的foreach创建了一个包含2行的列表。用户可以通过wysiwyg编辑器输入例如:' firstline-secondline,firstline-secondline'需要生成两行两行。在开始时,字符串在||上拆分为了表明列表在那里,然后该字符串以逗号分割,最后将其结果拆分为短划线( - )。

如何遍历爆炸数组以获得所需的结果?

再次作为例子:

// Explode the fulltext on || 
$explode = explode('||', $contentcr[0]['fulltext']);

echo $explode[0];

// Explode fulltext after || on commas
$explode2 = explode(',', $explode[1]);

$explode3 = explode('-', $explode2[1]);


foreach($explode2 as $list){
    $lijst .= ' 
    <div class="row item  wow fadeInUp animated" data-wow-duration="2s" style="visibility: visible; animation-duration: 2s; animation-name: fadeInUp;">
        <div class="col-md-2">
            <span class="icon-lamp px50 iconcolor"></span>
        </div>
        <div class="col-md-10 left">
            <div class="title black bold">'.strip_tags($list).'</div>
            <p>
                '.strip_tags($list).'
            </p>
        </div>
    </div>';
}

需要变成:

firstline-secondline,firstline-secondline

根据要求我现在在编辑器中拥有的内容:

<div class="col-md-10 left">
    <div class="title black bold">First line</div>
    <p>
        Second line
    </p>
</div>
<div class="col-md-10 left">
    <div class="title black bold">First line</div>
    <p>
        Second line
    </p>
</div>

编辑器会自动添加段落标记,以便您可以忽略这些标记。

3 个答案:

答案 0 :(得分:2)

我不完全确定你输入的形状,但在我看来你忽略了连字符的爆炸并在错误的地方做了

// Explode the fulltext on || 
$explode = explode('||', $contentcr[0]['fulltext']);

echo $explode[0];

// Explode fulltext after || on commas
$explode2 = explode(',', $explode[1]);

foreach($explode2 as $list){

    $lines = explode('-', $list);

    $lijst .= ' 
    <div class="row item  wow fadeInUp animated" data-wow-duration="2s" style="visibility: visible; animation-duration: 2s; animation-name: fadeInUp;">
        <div class="col-md-2">
            <span class="icon-lamp px50 iconcolor"></span>
        </div>
        <div class="col-md-10 left">
            <div class="title black bold">'. lines[0] .'</div>
            <p>
                '. $lines[1] .'
            </p>
        </div>
    </div>';
}

答案 1 :(得分:0)

在foreach中执行list($first,$second) = explode('-', $list);并使用$first $second打印行。

答案 2 :(得分:0)

如果我的问题是对的,你想要这个:

// Explode the fulltext on || 
$explode = explode('||', $contentcr[0]['fulltext']);

// Explode fulltext after || on commas
$explode2 = explode(',', $explode[1]);

foreach($explode2 as $list){

    $explode3 = explode('-', $list);

    $lijst .= ' 
    <div class="row item  wow fadeInUp animated" data-wow-duration="2s" style="visibility: visible; animation-duration: 2s; animation-name: fadeInUp;">
        <div class="col-md-2">
            <span class="icon-lamp px50 iconcolor"></span>
        </div>
        <div class="col-md-10 left">
            <div class="title black bold">'.strip_tags($explode3[0]).'</div>
            <p>
                '.strip_tags($explode3[1]).'
            </p>
        </div>
    </div>';
}