Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在数组中循环时返回的奇怪值_Php_Arrays_Foreach - Fatal编程技术网

Php 在数组中循环时返回的奇怪值

Php 在数组中循环时返回的奇怪值,php,arrays,foreach,Php,Arrays,Foreach,但循环呈现的值如下所示: [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

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

[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
        )

)

IDTitleDescription
1.
1.
1.
N
N
N
R
R
R
2.
2.
2.
F
F
F
R
R
R

知道是什么原因吗?我已经有一段时间没有使用纯php,而不是有自己数组处理功能的CMS了。

你有一个循环太多了:

<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>
$rows=getChallengeList();
错误日志(打印($rows,1));
?>
IDTitleDescription
试试:

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



这是不必要的。将其删除,然后使用
$row['loyalty\u challenges\u id']
等。

每个人只需一个

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

发生了什么事?

当您循环通过
$chal
时,您循环通过每个键,并且当您尝试访问
$chal['忠诚度挑战'id'


$rows[0]['忠诚度挑战\u id']['忠诚度挑战\u id']

翻译成

$rows[0]['忠诚度挑战\u id'][0]


这就是为什么每行都有第一个字母。

请停止使用短标记。你只需要一个
foreach
@njk-我被教导,如果你要有很多散布着php值的HTML,那么最好这样做,而不是呼出几百行HTML,这样你就可以呆在一个php标记内。@emmy Check这个问题:@Asok-根据这个答案,“正如ThiefMaster在评论中提到的,从PHP5.4开始,无论shorttags设置如何,标签在任何地方都是受支持的。这意味着它们可以安全地在可移植代码中使用,但这确实意味着依赖PHP5.4+”。这不是问题-我们拥有所有服务器,因此我们可以完全控制它们;相对于$chal字符串值,“忠诚度挑战id”值被转换为一个数字,给出0,因此它显示字符串偏移量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; ?>
    <?php foreach($row as $chal): ?>
    <tr>
        <td><?= $chal['loyalty_challenges_id']; ?></td>
        <td><?= $chal['title']; ?></td>
        <td><?= $chal['description']; ?></td>
    </tr>    
    <?php endforeach; ?>
<?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; ?>