Convert multidimensional array to string?

I am working on a small ajax application and should see if the values ​​generated in the background are expected. The value returned by the quet can be a rather complex multidimensional array, is there a way to convert it to a string so that it can be displayed with a warning?

Is there any other way to see these values?

Any advice is appreciated.

Thanks.

+7
php
source share
8 answers

print_r , var_dump or var_export are good candidates. when coding an ajax application, you can also look at json_encode .

+24
source share

If you want to show it using javascript, I would recommend json_encode() , everything else was covered by knittl's answer.

+4
source share
 <script type="text/javascript"> alert(<?=print_r($array)?>); </script> 
+1
source share

I found this feature 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/

+1
source share

The implode command returns an array as a string.

0
source share

I am working on a bunch of ajax applications .. in firefox I have an add-in called JSON view

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.

screenshot

it is being debugged now breaze

PS. know this Q is ancient and I really don’t respond to the original Q posters, but may be useful for someone else as well

0
source share

Here is a simple answer in 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); 
0
source share

you can also consider FirePHP http://www.firephp.org

-one
source share

All Articles