循环遍历数组,输出异常


looping through an array, wierd output

我有一个像这样的数组,

    Array
(
    [candidate_id] => 1
    [first_name] => Simon
    [surname] => Ainley
    [gender] => male
    [talent] => presenter
    [DOB] => 1987-06-12
    [Location] => Huddersfield
    [height] => 6' 3"
    [eyes] => blue
    [hair] => brown
    [hair_length] => short
    [accents] => Native english
    [training] => none
    [unions] => Actors guild
    [date_created] => 2011-10-11 00:00:00
)

然而,当我执行以下操作时,

<?php foreach ($results as $k => $v) : ?>
    <?php print_r($v); ?>
<?php endforeach; ?>

使用这个我得到输出,

1 S A m p 1 H 6 b b s N n A 2

当您尝试回显实际上是字符串的数组元素时,就会发生这种情况。如:

$string = "hello";
echo $string[1]; // echos "e"

确保$results的结构是你认为的那样,并且你在正确的变量上使用了print_r()。

参见:通过字符Docs访问和修改字符串。