如何进行动态循环?

时间:2016-08-04 01:43:52

标签: php html arrays

我有这个PHP数组:

echo "<pre>";
print_r($notifications);

/* output:

Array
(
    [0] => Array
        (
            [score] => 120
            [type] => 5
            [post_id] => 1
            [subject] => a subject
            [range_day] => today
        )

    [1] => Array
        (
            [score] => 6
            [type] => 4
            [post_id] => 2
            [subject] => a subject
            [range_day] => today
        )

    [2] => Array
        (
            [score] => 2
            [type] => 4
            [post_id] => 3
            [subject] => a subject
            [range_day] => yesterday
        )
)
*/

这是预期输出:

<ul>
    <li>
        <p class="subject">a subject<span> | type:5</span></p>
        <div class="score">Your score: 120</div>
        <a href="/1/a-subject"> 
        <span class="date">today</span>
    </li>
    <li>
        <p class="subject">a subject<span> | type:4</span></p>
        <div class="score">Your score: 6</div>
        <a href="/2/a-subject"> 
        <span class="date">today</span>
    </li>
    <li>
        <p class="subject">a subject<span> | type:4</span></p>
        <div class="score">Your score: 2</div>
        <a href="/3/a-subject"> 
        <span class="date">yesterday</span>
    </li>
</ul>

此处还有我的当前代码:

$html = "<ul>";
foreach ( $notification as $item1 ) {

    $html .= "<li>";
    foreach ( $item as $item2 ) {

        // in here I need to put different tags for different $item2. How?
    }
    $html .= "</li>";

}
$html = "</ul>";

如您所见,我需要为每个密钥使用多个不同的标签。我该如何管理?我的意思是我需要将第一个密钥包围到<div>标记中,将第二个密钥包围到<span>标记中等等。我该怎么做?

4 个答案:

答案 0 :(得分:1)

删除一个循环并手动处理元素:

<?php
$upload_dir = "upload/";
$img = $_POST['hidden_data'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = $upload_dir . mktime() . ".png";
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>

答案 1 :(得分:1)

这可能会对您有所帮助

<ul>
 <?php 
   foreach ( $notifications as $item ) { ?>
    <li>
     <p class="subject"><?php echo $item['subject']; ?><span> | type:    <?php echo $item['type']; ?></span></p>
    <div class="score">Your score: <?php echo $item['score']; ?></div>
    <a href="/<?php echo $item['post_id']; ?>/<?php echo $item['subject']; ?>"> 
    <span class="date"><?php echo $item['range_day']; ?></span>
</li>
<?php
 }
?>

答案 2 :(得分:1)

执行以下简单操作:

$html = "<ul>";
foreach ( $notification as $item1 ) {
foreach ( $item as $item2 ) {
    $html .= "<li>";
    $html .= "<p class=subject> ${item1['subject']} </p>";
    $html .= "<div class=score>Your score: ${item1['score']}</div>"
    ............//others logic
    $html .= "</li>";
}
$html .= "</li>";
$html .= "</ul>";

答案 3 :(得分:0)

我首选的方法是尽可能地将PHP逻辑与HTML分开。这使得更简单的html:

$template = <<< 'TXT'
<li>
   <p class="subject">{{subject}}<span> | type:{{type}}</span></p>
   <div class="score">Your score: {{score}}</div>
   <a href="/{{post_id}}/{{no_space_subject}}"> 
   <span class="date">{{range_day}}</span>
</li>
TXT;
$links = '';

foreach ($notifications as $n){
    $n['no_space_subject'] = str_replace(' ','-',$n['subject']);
    //replace {{VALUE}} with $n[VALUE] in the template
    $links .= preg_replace_callback('/{{([^}]+)}}/',
            function($m) use ($n){return $n[$m[1]];},
            $template
        );
}

然后在你的html中,你可以输出结果:

<ul><?=$links?></ul>

Live demo