循环遍历数组时返回奇怪的值

时间:2013-01-11 16:49:32

标签: php arrays foreach

$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 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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; ?>

发生了什么事?

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

相同

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

被翻译为

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

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

答案 2 :(得分: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; ?>

答案 3 :(得分: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']等。