【问题标题】:Strange values returned when looping through array遍历数组时返回的奇怪值
【发布时间】:2013-01-11 16:49:32
【问题描述】:
$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。

【问题讨论】:

  • 请停止使用短标签。
  • 您只需要一个foreach
  • @njk - 有人告诉我,如果您要使用大量带有 php 值的 HTML,最好这样做,而不是回显数百行 html,这样您就可以留在里面一个 php 标签。
  • @Asok - 根据那个答案,“正如 ThiefMaster 在 cmets 中提到的那样,从 PHP 5.4 开始,= ... ?> 标签在任何地方都受支持,无论短标签设置如何。这应该意味着它们在可移植代码中使用是安全的,但这确实意味着对 PHP 5.4+ 的依赖”。不是问题 - 我们拥有所有服务器,因此我们可以完全控制它们。

标签: php arrays foreach


【解决方案1】:

你的循环太多了:

$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>

【讨论】:

  • 解释:当使用$chal['loyalty_challenges_id'];针对 $chal 字符串值,'loyalty_challenges_id' 值被强制转换为数字,给出 0,因此它显示字符串偏移量 0 处的字符,即字符串中的第一个字符
  • 谢谢。我可以发誓我已经尝试过了,并得到了空字符串,但我想没有。
【解决方案2】:

你只需要一个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]

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

【讨论】:

    【解决方案3】:

    试试:

    <?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; ?>
    

    【讨论】:

      【解决方案4】:
          <?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'] 等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-21
        • 2021-12-05
        • 1970-01-01
        • 2015-05-09
        • 1970-01-01
        相关资源
        最近更新 更多