Aureole Posted October 11, 2007 Share Posted October 11, 2007 I know I can use print_r($_SESSION); or something similiar but that outputs something like... Array ( [ 0 ] => Something [ 1 ] => Something else ) I thought I might be able to use something like...: <?php $a = $_SESSION[]; foreach($a as $b) { echo $b; } ?> But I get: Fatal error: Cannot use [] for reading in /home/veraci7y/public_html/rev/index.swr3 on line 18 Quote Link to comment https://forums.phpfreaks.com/topic/72703-solved-print_r-foreach/ Share on other sites More sharing options...
kenrbnsn Posted October 11, 2007 Share Posted October 11, 2007 Don't put in the "[]". This should work fine: <?php $a = $_SESSION; foreach($a as $b) { echo $b; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/72703-solved-print_r-foreach/#findComment-366666 Share on other sites More sharing options...
Aureole Posted October 11, 2007 Author Share Posted October 11, 2007 Ah yes it does, but still it's not showing how I want it to... Ok here's the thing... if I use print_r I get something like this: Array ( [mem_lname] => 32 char md5 here [mem_dname] => Aureole [mem_pass] => 32 char md5 here [mem_email] => My Email Address [mem_id] => 12 [logged_in] => 1 [mem_group] => 1 [is_admin] => 1 [is_supermod] => 1 ) If I use foreach($a as $b) { echo $b; } I get something like this... 32 char md5 Aureole 32 char md5 here My Email Address 12 1 1 1 1 But without the spaces of course... Now how I'd like for it to show is... mem_lname: 32 char md5 here mem_dname: Aureole mem_pass: 32 char md5 here mem_email: My Email Address mem_id: 12 logged_in: 1 mem_group: 1 is_admin: 1 is_supermod: 1 Is there anyway to do this without actually echoing each of the session variables one by one as there's quite a few more than I posted here... <?php echo('mem_lname: '.$_SESSION['mem_lname'].''); echo('mem_dname: '.$_SESSION['mem_dname'].''); ?> I DON'T want to do it like that... I want it to do it dynamically so if (well when) I add new session variables I won't have to change it. Thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/72703-solved-print_r-foreach/#findComment-366670 Share on other sites More sharing options...
kenrbnsn Posted October 11, 2007 Share Posted October 11, 2007 You want to do something like: <?php foreach($_SESSION as $key => $val) echo $key . ' : ' . htmlentities($val,ENT_QUOTES) . "<br>\n"; ?> You want to use the htmlentities() function so that if you have any HTML in the values, it will be displayed, not interpreted. Ken Quote Link to comment https://forums.phpfreaks.com/topic/72703-solved-print_r-foreach/#findComment-366672 Share on other sites More sharing options...
Aureole Posted October 11, 2007 Author Share Posted October 11, 2007 That works a charm and I think I understand it... thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/72703-solved-print_r-foreach/#findComment-366673 Share on other sites More sharing options...
Aureole Posted October 11, 2007 Author Share Posted October 11, 2007 Is it possible to do something similiar with all user defined constants... like I have a bunch of constants I defined and I'd like to do something similar with them... They're in a file called config.swr3 and each is on it's own line so maybe there's a way to go through each line and do it... Quote Link to comment https://forums.phpfreaks.com/topic/72703-solved-print_r-foreach/#findComment-366679 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.