Jump to content

[SOLVED] print_r / foreach


Aureole

Recommended Posts

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

 

:(

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.