This question already has answers here:
Is there a pretty print for PHP?
(31 answers)
Closed 7 months ago.
How do i print an Array readable? print_r($array); and var_dump($array); both produce some very ugly clutter of that $array. Ok it is what it says, it prints that array, but i'd like to have some well formated print of that array, how do i do that?
echo '<pre>';
var_dump($array);
echo '</pre>';
Or to a better yet performance, use xDebug with
html_colors = on.
html_colors can be found on php.ini file.
xdebug is from http://xdebug.org/download.php
With xDebug and Colors you don't need to use
echo '<pre>';
echo '</pre>';
it's beautiful as-is.
Actually, it is well-formatted but HTML ignores line breaks and double spaces.
You just have to view the page's source code (CTRL+U or right-click > view source code)
You can wrap your var_dump() output in <pre> to preserve the monospacing:
echo "<pre>" . var_dump($array) . "</pre>";
echo "<pre>".print_r($array, true)."</pre>";
try this code
$myArray=array("a","b","c");
foreach($myArray as $A)
{
echo $A."<br/>"; // you can use any style here like table, div span etc...
}
I like:
highlight_string(var_export($array, true));
var_export() is usable (copy/paste) PHP code as well.
if you want to see it well formatted, you can encode it to json object and print it pretty :)
You have to do is:
echo json_encode($array,JSON_PRETTY_PRINT); //This needs PHP 5.3 at least.
Related
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Pretty-Printing JSON with PHP
I'm working on a script that creates a JSON file. Right now I'm just using json_encode (PHP 5.2.x) to encode an array into JSON output. Then I print the returned value to a file and save it. Problem is that the client wants to be able to open these JSON files for readability, so I'd like to add line breaks in and "pretty print" the JSON output. Any ideas on how to do this? My only other alternative that I can see is to not use json_encode at all and just write the file contents manually and add in my own line breaks for each line.
Here's what I get:
{"product_name":"prod1","val1":1,"val2":8}
Here's what I want:
{
"product_name":"prod1",
"val1":1,
"val2":8
}
I suppose I could also just replace every comma with a command followed by a \n, and same for the brackets... Thoughts?
PHP has JSON_PRETTY_PRINT option since 5.4.0 (release date 01-Mar-2012).
This should do the job:
$json = json_decode($string);
echo json_encode($json, JSON_PRETTY_PRINT);
See http://www.php.net/manual/en/function.json-encode.php
Note: Don't forget to echo "<pre>" before and "</pre>" after, if you're printing it in HTML to preserve formatting ;)
Hmmm $array = json_decode($json, true); will make your string an array which is easy to print nicely with print_r($array, true);
But if you really want to prettify your json... Check this out
Here's a function to pretty up your json:
pretty_json
Normally, I would use print_r function to do the task, but I still need to manually add the <pre> tags. I am wondering if there is a function in PHP that can do it automatically, something like print_pre($array).
print_r has a second param, if true it returns the value.
echo '<pre>'.print_r($array, true).'</pre>';
Thers's no such function for this task instead you can use
echo '<pre>';
print_r($array);
echo '</pre>';
To make it as short as possible.
The previous answers are correct, but if you would like to use advanced debug tool, you can install xdebug and it can be automatically format var_dump.
You can find more information about xdebug here: https://xdebug.org/
I'm trying to learn JSON and PHP using API.
My problem is that I can't get out the data of example Array 0, the output as can seen at bottom if you visit the link, just Array googled as mutch as I can but kinda new on the json array.
$json_data=json_decode($json_array,true);
print_r($json_data);
echo "<br>";
echo $json_data['total'],"<br>";
echo $json_data['skipped'],"<br>";
echo $json_data['count'],"<br>";
echo $json_data['0'],['name'],"<br>";
Based on your comment, I think I see what you want to do. You're trying to access the second level of your array incorrectly. You don't put anything between the two bracket sets. You also don't need to put the 0 in quotes, as it is not a string. I think you want this:
$json_data=json_decode($json_array,true);
print_r($json_data);
echo "<br>";
echo $json_data['total'],"<br>";
echo $json_data['skipped'],"<br>";
echo $json_data['count'],"<br>";
echo $json_data[0]['name'],"<br>"; // Line changed, removed comma and quotes.
EDIT: Note that the [0]['name'] is inside the hits value, so you'll probably want this instead:
echo $json_data['hits'][0]['name'];
How can I in an API echo results in a new array?
example:
echo implode('[]',$resultArray);
result:
$resultArray
requested result:
[$resultArray]
Since you're implementing an API the best pracrice will be to output your results in a format that's readable and understandable by multiple programming languages. JSON is the way to go. Read about it here and here
To echo an array, or anything not just an array, in PHP in a JSON format use:
echo json_encode($data);
where $data holds the output
echo json_encode($resultArray);
Assuming you want to keep the data in the response an array to the requester.
If not, your example will work but I'd suggest something easy to parse on the requester's side like a CSV value using commas: echo implode(',', $resultArray);
if you're looking to output the string '[$resultArray]', it'll be something like echo '[$'.print_var_name($resultArray).']'. Not sure why you'd need it like this, but your question is a little bit vague/confusing on exactly what you're looking for.
You could use:
echo "<pre>"; var_dump($array); echo "</pre>";
This way you will get a nice preformatted result of var_dump.
try this
$array = json_decode(json_encode($resultArray), true);
or
Print_r();
I'm trying to parse the result in PHP from a SOAP request.
Sometimes the response can be in a single object, but sometimes it can be multiple array results, and then my PHP breaks.
If it's only a single result (Object), then I have no problem to print it out:
<?php
echo $nuvarandetidigareagarefraga_output->return->foregaendeAgare->fv12->FV12_Agare_fr_o_m;
echo $nuvarandetidigareagarefraga_output->return->foregaendeAgare->fg01->FG01_Personnummer;
echo $nuvarandetidigareagarefraga_output->return->foregaendeAgare->fg01->FG01_Namn;
echo $nuvarandetidigareagarefraga_output->return->foregaendeAgare->fg02->FG02_Adress;
echo $nuvarandetidigareagarefraga_output->return->foregaendeAgare->fg02->FG02_Postnummer;
echo $nuvarandetidigareagarefraga_output->return->foregaendeAgare->fg02->FG02_Postort;
?>
But when it's multiple responses (Array), I have to use something like:
<?php
echo $nuvarandetidigareagarefraga_output->return->foregaendeAgare[0]->fg01->FG01_Namn;
?>
Is it possible to make loop of some kind instead to always show the results?
Are you looking for this simple solution:
foreach($nuvarandetidigareagarefraga_output->return->foregaendeAgare as $element){
print $element->fg01->FG01_Namn;
}
?
If yes then please consult php manual: http://php.net/manual/en/language.types.array.php and http://php.net/manual/en/language.control-structures.php section about loops.