15

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?

Is there any other way of seeing these values?

Any advice appreciated.

Thanks.

8 Answers 8

30

print_r, var_dump or var_export are good candidates. When writing a REST service, you might also want to look at json_encode.

Sign up to request clarification or add additional context in comments.

1 Comment

output of var_export can be saved to a variable: $a = array (1, 2, array ("a", "b", "c")); $b = var_export($a, true); echo $b;
7

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

Comments

4

i work on a bunch of ajax apps.. in firefox i have an "add-on" 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

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

Comments

2
<script type="text/javascript">
alert(<?=print_r($array)?>);
</script>

Comments

1

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/

Comments

1

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);

2 Comments

several typos here...within implode_recur, the reference to implode_r should be implode_recur.Also change the two instances of $out to $output. This will also addthe separator string before the first element in the array ...">>a>>b>>" which may not be desirable in all cases.
This works. Just need to update the $out variables to $output.
-1

The implode command returns an array as a string.

1 Comment

This is fine for a single array, but does not work out of the box with multidimensional arrays. You would need to loop through each array.
-3

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.