PHP - 将多维数组转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1764500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Convert a multidimensional array to string?
提问by Dan
I'm working on a small ajax application and need to see if the values being generated in the background are what is expected. The value returned by the quest can be quite a complex multidimensional array, is there a way to convert this into a string so that it can be shown with alert?
我正在开发一个小型 ajax 应用程序,需要查看在后台生成的值是否符合预期。任务返回的值可能是一个相当复杂的多维数组,有没有办法将其转换为字符串,以便它可以与警报一起显示?
Is there any other way of seeing these values?
有没有其他方式可以看到这些值?
Any advice appreciated.
任何建议表示赞赏。
Thanks.
谢谢。
回答by knittl
print_r, var_dumpor var_exportare good candidates. while coding an ajax application, you might also want to look at json_encode.
print_r,var_dump或者var_export是很好的候选人。在编写 ajax 应用程序时,您可能还想查看json_encode.
回答by soulmerge
If you want to show it with javascript, I would recommend json_encode(), everything else has been covered by knittl's answer.
如果你想用 javascript 显示它,我会推荐json_encode()knittl 的答案已经涵盖了其他所有内容。
回答by WilliamStam
i work on a bunch of ajax apps.. in firefox i have an "add-on" called JSON view
我在一堆ajax应用程序上工作..在firefox中我有一个名为JSON视图的“附加组件”
then all my projects have a function
那么我所有的项目都有一个功能
function test_array($array) {
header("Content-Type: application/json");
echo json_encode($array);
exit();
}
so whenever i want to see what the output is i just go test_array($something)and it shows me the results.
所以每当我想看看输出是什么时,我就去test_array($something)看看结果。


its made debugging a breaze now
它让调试变得轻而易举
PS. know this Q is ancient and im not really answering the origional posters Q but might be useful for someone else aswell
附注。知道这个 Q 很古老,我并没有真正回答原始海报 Q,但可能对其他人也有用
回答by Roch
<script type="text/javascript">
alert(<?=print_r($array)?>);
</script>
回答by Ahmad Balavipour
I found this function useful:
我发现这个功能很有用:
function array2str($array, $pre = '', $pad = '', $sep = ', ')
{
$str = '';
if(is_array($array)) {
if(count($array)) {
foreach($array as $v) {
$str .= $pre.$v.$pad.$sep;
}
$str = substr($str, 0, -strlen($sep));
}
} else {
$str .= $pre.$array.$pad;
}
return $str;
}
from this address: http://blog.perplexedlabs.com/2008/02/04/php-array-to-string/
从这个地址:http: //blog.perplexedlabs.com/2008/02/04/php-array-to-string/
回答by kachmi
Here is a simple answer in PHP:
这是一个简单的 PHP 答案:
function implode_recur($separator, $arrayvar) {
$output = "";
foreach ($arrayvar as $av)
if (is_array ($av))
$out .= implode_recur($separator, $av); // Recursive array
else
$out .= $separator.$av;
return $out;<br>
}
$result = implode_recur(">>",$variable);
回答by user187291
you can also consider FirePHP http://www.firephp.org
你也可以考虑 FirePHP http://www.firephp.org

![php SQLSTATE[HY000]:一般错误:1005 无法创建表 - Laravel 4](/res/img/loading.gif)