将数组元素拆分为两列

时间:2015-01-15 22:38:35

标签: php arrays split

请在PHP代码下面帮助我:

我在下面有数组:

$array = array(
    array(
        "tile"=> "title 1",
        "description" => "description 1"
    ),
    array(
        "tile" => "title 2",
        "description" => "description 2"
    ),
    array(
        "tile" => "title 3",
        "description" => "description 3"
    )
);

我想要输出HTML如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <!-- Latest compiled and minified CSS & JS -->
  <link rel="stylesheet" media="screen" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
  <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
  <script src="//code.jquery.com/jquery.js"></script>
  <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-lg-4 class_1">
            <div class="title">
                title 1
            </div>
            <div class="title">
                title 2
            </div>
            <div class="title">
                title 3
            </div>                        
        </div>
        <div class="col-lg-8 class_2">
            <div class="description">
                description 1    
            </div>
            <div class="description">
                description 2    
            </div>
             <div class="description">
                description 3    
            </div>                       
        </div>
    </div>
</div>     
</body>
</html>

如何使用foreach循环并将标题和描述分成两个列(标题1,标题2,标题3 仅在一个类标题中,以及描述1,描述2,描述3 仅在一个类描述中),输出如下:

title 1  |  description 1
-------------------------------
title 2  |  description 2
-------------------------------
title 3  |  description 3
-------------------------------

如果说明1 说明2 为空,我希望输出为:

title 1  |  description 2
-------------------------------
title 2  |  description 3
-------------------------------
title 3  |
-------------------------------

(&#34; 说明2 &#34;可以填写&#34; 说明1 &#34;如果&#34; 描述1 &#34;为空)

title 1  |  description 1
-------------------------------
title 2  |  description 3
-------------------------------
title 3  |
-------------------------------

最后,如何实现这个目标:

<div class="container">
    <div class="row">
        <div class="col-lg-4 class_1">
            <div class="title">
                title 1
            </div>
            <div class="title">
                title 2
            </div>
            <div class="title">
                title 3
            </div>                        
        </div>
        <div class="col-lg-8 class_2">
            <div class="description">
                description 1    
            </div>
            <div class="description">
                description 2    
            </div>
             <div class="description">
                description 3    
            </div>                       
        </div>
    </div>
</div> 

感谢您的帮助。 非常感谢你。

1 个答案:

答案 0 :(得分:0)

在循环之前拆分数组。

<?php
   $titles = array();
   $description = array();
   foreach ($array as $value) {
       if ($value['tile']) {
          $titles[] = $value['tile'];
       }
       if ($value['description']) {
          $descriptions[] = $value['description'];
       }
   }
?>
<?php foreach ($titles as $i => $title): ?>
    <div class="row">
        <div class="col-lg-4 class_1">
           <div class="title">
              <?php echo $title ?>
           </div>
        </div>
        <div class="col-lg-8 class_2">
           <div class="description">
              <?php if (isset($descriptions[$i])): ?>
              <?php echo $descriptions[$i]; ?>   
              <?php endif; ?>  
          </div>
        </div>
    </div>
<?php endforeach; ?>