如何在PHP数组中使用echo返回字符串 [英] How to use echo in php array to return as a string
问题描述
我正在尝试返回一个字符串数组。我这样做:
I am trying to return an array of strings. I do this:
$errors[] = toolbarCheckCreds($_COOKIE['uname'], $_COOKIE['pword'], $_COOKIE['rememberMe']);
echo $errors[0];
这在最后的函数中:
return $errors;
,我设置了这样的错误:
and I set an error like this:
$errors[] = "error goes here!";
基本上,当我返回数组并回显它时,它会提供以下输出:
Basically, when I return my array, and echo it, it gives me the following output:
Array
推荐答案
使用 PHP爆破,将您的Array转换为可以回显的字符串。在数组上使用echo只会显示数据类型。
Use PHP implode to convert your Array to a string that you can echo. Using echo on an array will just display the data type.
return implode(' ', $errors);
如果要使用除空格以外的定界符来分隔错误,只需替换空格即可。第一个参数:
If you want to separate the errors with a delimiter other than a space, just replace the space in the first parameter:
return implode(' :: ', $errors);
例如,如果您的错误数组包含三个值:
For example, if your errors array contained three values:
[ "Invalid data" , "404" , "Syntax error" ]
然后,如果在结果上运行 echo
,则字符串(如果使用::的话)将如下所示:
then your string, if you used the ::, would look like this when you run echo
on the result:
Invalid data :: 404 :: Syntax error
有关另一个示例,请参见我包含的参考链接。
See the reference link I included for another example.
这篇关于如何在PHP数组中使用echo返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!