错误消息中的Echo数组值

时间:2011-04-19 18:59:35

标签: php arrays

我有一个数组,我想将数组的值显示为错误消息的一部分。但当然,当我执行下面的代码时,我只是在最后得到了带有数组的错误消息。请帮忙

$matches = array("2","35","27");

现在我只想在错误消息中显示值。

if (isset($matches)){

    $error_message = "The following numbers match: " . $matches;

}

echo $error_message;

结果:

The following numbers match: 2 35 27

2 个答案:

答案 0 :(得分:2)

我能想到的最简单的方法是使用implode。您可能想要进行is_array检查,但这应该有效。

$error_message = "The following numbers match: " . implode(' ', $matches);

答案 1 :(得分:0)

试试这段代码:

<?php
$matches = array("2","35","27");
if (isset($matches)){
    $error_message = "The following numbers match: " . var_export($matches, true);
}
echo $error_message . "\n";

?>

<强>输出

The following numbers match: array (
  0 => '2',
  1 => '35',
  2 => '27',
)

查看var_export manual here