rxgvhqkrywq6cxdjoar Posted April 1, 2010 Share Posted April 1, 2010 Hey everyone, PHP is not co-operating today! I'm having issues creating new lines... This is my code: <?php $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z')); print_r ($a); echo "first line". "\n"; echo "second line\n"; echo "third line \n"; echo "..."; ?> The expected output is: Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) ) first line second line third line ... The actual output is: Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) ) first line second line third line ... Which is hardly readable when using large arrays! Any help us much appreciated Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/197269-n-not-working-arrays-not-displaying-properly/ Share on other sites More sharing options...
Alex Posted April 1, 2010 Share Posted April 1, 2010 If you view the source of the output you'll see it is as expected. You're looking for HTML <br /> line breaks. If you want the print_r to display the line breaks in html as well, use the second parameter in print_r to return the output and then perform nl2br on that to convert character \n to <br />. echo nl2br(print_r($a, true)); Quote Link to comment https://forums.phpfreaks.com/topic/197269-n-not-working-arrays-not-displaying-properly/#findComment-1035421 Share on other sites More sharing options...
freakstyle Posted April 1, 2010 Share Posted April 1, 2010 or: <?php echo '<pre>'; print_r($some_array); exit('printed nicely as source me'); ?> or you can create your own print function to reuse it elsewhere. Quote Link to comment https://forums.phpfreaks.com/topic/197269-n-not-working-arrays-not-displaying-properly/#findComment-1035567 Share on other sites More sharing options...
Daniel0 Posted April 1, 2010 Share Posted April 1, 2010 You need to read up on content types. If you send data as text/html to a web browser, it'll regard it as HTML. In HTML, consecutive whitespace is ignored. For example, a billion space characters would still only be represented as a single space. Quote Link to comment https://forums.phpfreaks.com/topic/197269-n-not-working-arrays-not-displaying-properly/#findComment-1035569 Share on other sites More sharing options...
rxgvhqkrywq6cxdjoar Posted April 6, 2010 Author Share Posted April 6, 2010 freakstyle, you are a saint. thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/197269-n-not-working-arrays-not-displaying-properly/#findComment-1037818 Share on other sites More sharing options...
salathe Posted April 6, 2010 Share Posted April 6, 2010 As Daniel said, send the output as plain text rather than HTML and you won't have to play around with writing HTML tags to show the text in a monospace font. Use something like the following, before your print_r: header('Content-Type: text/plain; charset=utf-8'); Edit: Can't speel correctely!! Quote Link to comment https://forums.phpfreaks.com/topic/197269-n-not-working-arrays-not-displaying-properly/#findComment-1037848 Share on other sites More sharing options...
Chris92 Posted April 6, 2010 Share Posted April 6, 2010 Right click, view source. Quote Link to comment https://forums.phpfreaks.com/topic/197269-n-not-working-arrays-not-displaying-properly/#findComment-1037851 Share on other sites More sharing options...
rxgvhqkrywq6cxdjoar Posted April 6, 2010 Author Share Posted April 6, 2010 salathe another great solution that solves my problem. thanks all. Quote Link to comment https://forums.phpfreaks.com/topic/197269-n-not-working-arrays-not-displaying-properly/#findComment-1037854 Share on other sites More sharing options...
salathe Posted April 7, 2010 Share Posted April 7, 2010 LOL, it wasn't my solution. Thank Daniel. [ot]Do you only read code, rather than taking notice of all those other less-colourful words? Not a jab, just an observation.[/ot] Quote Link to comment https://forums.phpfreaks.com/topic/197269-n-not-working-arrays-not-displaying-properly/#findComment-1038241 Share on other sites More sharing options...
rxgvhqkrywq6cxdjoar Posted April 7, 2010 Author Share Posted April 7, 2010 I had no idea what Daniel was referring to until you enlightened me. Thanks all. Over and Out. rx Quote Link to comment https://forums.phpfreaks.com/topic/197269-n-not-working-arrays-not-displaying-properly/#findComment-1038287 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.