2
$rows = getChallengeList();
error_log(print_r($rows, 1));
?>
<table border="1">
    <tr><th>ID</th><th>Title</th><th>Description</th></tr>
    <?php foreach ($rows as $row): ?>
        <?php foreach($row as $chal): ?>
        <tr>
            <td><?= $chal['loyalty_challenges_id']; ?></td>
            <td><?= $chal['title']; ?></td>
            <td><?= $chal['description']; ?></td>
        </tr>    
        <?php endforeach; ?>
    <?php endforeach; ?>
</table>

error_log 返回:

[11-Jan-2013 10:44:27] Array
(
    [0] => Array
        (
            [loyalty_challenges_id] => 1
            [title] => New Customer Special
            [description] => Reward new customers with a free order of breadsticks after placing their second order during their first 30 days 
        )

    [1] => Array
        (
            [loyalty_challenges_id] => 2
            [title] => Frequent Flyer Special
            [description] => Reward long-time customers who order often with a free pizza
        )

)

但循环呈现的值如下所示:

<table border="1">
    <tr><th>ID</th><th>Title</th><th>Description</th></tr>
    <tr>
        <td>1</td>
        <td>1</td>
        <td>1</td>
    </tr>    
    <tr>
        <td>N</td>
        <td>N</td>
        <td>N</td>
    </tr>    
    <tr>
        <td>R</td>
        <td>R</td>
        <td>R</td>
    </tr>    
    <tr>
        <td>2</td>
        <td>2</td>
        <td>2</td>
    </tr>    
    <tr>
        <td>F</td>
        <td>F</td>
        <td>F</td>
    </tr>    
    <tr>
        <td>R</td>
        <td>R</td>
        <td>R</td>
    </tr>    
</table>

知道什么会导致这种情况吗?自从我使用直接 php 而不是拥有自己的数组处理功能的 CMS 以来,已经有一段时间了。

4

4 回答 4

5

你有一个循环太多:

$rows = getChallengeList();
error_log(print_r($rows, 1));
?>
<table border="1">
    <tr><th>ID</th><th>Title</th><th>Description</th></tr>
    <?php foreach ($rows as $chal): ?>
        <tr>
            <td><?= $chal['loyalty_challenges_id']; ?></td>
            <td><?= $chal['title']; ?></td>
            <td><?= $chal['description']; ?></td>
        </tr>    
    <?php endforeach; ?>
</table>
于 2013-01-11T16:52:25.377 回答
1

你只需要一个foreach

<?php foreach ($rows as $row): ?>
    <tr>
        <td><?php echo $row['loyalty_challenges_id']; ?></td>
        <td><?php echo $row['title']; ?></td>
        <td><?php echo $row['description']; ?></td>
    </tr>    
<?php endforeach; ?>

发生了什么 ?

当您循环时,$chal您会循环每个键,并且当您尝试访问时 $chal['loyalty_challenges_id'];

是一样的

$rows[0]['loyalty_challenges_id']['loyalty_challenges_id']

这被翻译成

$rows[0]['loyalty_challenges_id'][0]

这就是为什么你得到每行的第一个字母。

于 2013-01-11T16:52:58.980 回答
0

尝试 :

<?php foreach ($rows as $row): ?>
    <tr>
        <td><?php echo $row['loyalty_challenges_id']; ?></td>
        <td><?php echo $row['title']; ?></td>
        <td><?php echo $row['description']; ?></td>
    </tr>    
<?php endforeach; ?>
于 2013-01-11T16:52:35.867 回答
0
    <?php foreach($row as $chal): ?>
    <tr>
        <td><?= $chal['loyalty_challenges_id']; ?></td>
        <td><?= $chal['title']; ?></td>
        <td><?= $chal['description']; ?></td>
    </tr>    
    <?php endforeach; ?>

这个 foreach 是不需要的。删除它,然后使用$row['loyalty_challenges_id']等。

于 2013-01-11T16:52:47.047 回答