/    Sign up×
Community /Pin to ProfileBookmark

Session Arrays and echo a complete single entry

Good day to you all,
I’m working around array and session and I was wondering how can I retrieve all array that are from level one.

This array is a file array.

I’m working with a multidimensional array.

Here is my code :

[code=php]

<?php

session_start();

function recur_dir($dir)
{
$dirlist = opendir($dir);
while ($file = readdir ($dirlist))
{
if ($file != ‘.’ && $file != ‘..’)
{
$newpath = $dir.’/’.$file;
$level = explode(‘/’,$newpath);
if (is_dir($newpath))
{
$mod_array[] = array(
‘level’=>count($level)-1,
‘path’=>$newpath,
‘name’=>end($level),
‘kind’=>’dir’,
‘mod_time’=>filemtime($newpath),
‘content’=>recur_dir($newpath) );
}else{
$mod_array[] = array(
‘level’=>count($level)-1,
‘path’=>$newpath,
‘name’=>end($level),
‘kind’=>’file’,
‘mod_time’=>filemtime($newpath),
‘size’=>filesize($newpath) );
}
}
}
closedir($dirlist);

ksort($mod_array);

$_SESSION[‘listimages’]=$mod_array;

return $mod_array;
}

echo ‘<pre>’;
print_r(recur_dir(‘Art’));
echo ‘</pre>’;

?>

[/code]

[code=php]

<?php
session_start();
Print_r ($_SESSION);
echo “<p>”;

//echo a single entry from the array
echo $_SESSION[‘listimages’][‘level’][2];
?>

[/code]

Thanks !

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@tbirnsethApr 05.2009 — 
//echo a single entry from the array

echo $_SESSION['listimages']['level'][2];
[/quote]


I don't see where you're setting the associative array 'level'.

change it to:
echo $_SESSION['listimages'][2];
and I think you'll get what you want (assuming there have been two instances of it set in your called function.
Copy linkTweet thisAlerts:
@PeuplarchieauthorApr 05.2009 — and what about if I want to display all the ones that are in level 2
Copy linkTweet thisAlerts:
@tbirnsethApr 05.2009 — You're not giving me a lot to go on here....

Assign your values to an array and use a loop (or specific index) to access.

My point was that you do not have an assignment of 'level' in your function.
Copy linkTweet thisAlerts:
@PeuplarchieauthorApr 05.2009 — how can I achieve that ?
Copy linkTweet thisAlerts:
@tbirnsethApr 05.2009 — Add a print_r($_SESSION['listimages']) at the end of your function. It should clearly show you the structure you end up with. Right now, I believe you would be assigning everything to the top level. You might change $_SESSION['listimages'] to $_SESSION['listimages']['content'] for consistency.
Copy linkTweet thisAlerts:
@PeuplarchieauthorApr 06.2009 — $_SESSION['listimages']['content']

I'm fairely new to array and session together, can you explain a bit more, do I have to make one for each key ?
Copy linkTweet thisAlerts:
@tbirnsethApr 06.2009 — Okay, here's how "I" would do it. But it's not tested nor even syntax checked. But you should be able to see what I've done. Essentially, I made the file and dir structures the same with a file always having content be equal to array(). This eliminates a few of your structure elements. I also make 'level' be part of the recursion. I.e. each time you recurse, level is incremented. To me, this is much more natural. I then usort() the dir and file array elements at each level based on the name.

This should give you a fairly clean structure that makes sense. Let me know how it works for you.

<i>
</i>&lt;?php

session_start();

function ar_cmp($ar1, $ar2) {
return strcmp($ar1['name'], $ar2['name']);
}

function recur_dir($dir, $level=0)
{
$dirlist = opendir($dir);
while ($file = readdir ($dirlist))
{
if ($file != '.' &amp;&amp; $file != '..')
{
$newpath = $dir.'/'.$file;
// Use level from the recursion level
// $level = explode('/',$newpath);
if (is_dir($newpath))
{
$mod_array[$level]['dirs'][] = array(
// 'level'=&gt;count($level)-1, // not needed
'path'=&gt;$newpath,
'name'=&gt;end($level),
// 'kind'=&gt;'dir', // not needed
'mod_time'=&gt;filemtime($newpath),
'size'=&gt; 0, // keep data structures consistitent between dirs and files
// recurse on directories only and keep results at this level
'content'=&gt;recur_dir($newpath, $level + 1) );
}else{
$mod_array[$level]['files'][] = array(
// 'level'=&gt;count($level)-1, // not needed
'path'=&gt;$newpath,
'name'=&gt;end($level),
// 'kind'=&gt;'file', // not needed
'mod_time'=&gt;filemtime($newpath),
'size'=&gt;filesize($newpath),
'content'=&gt;array() ); // keep data structures consistitent between dirs and files
}
}
}
closedir($dirlist);

// ksort($mod_array); // not sure what you're wanting to sort on.
if( count($mod_array['dirs']) &gt; 1 )
usort($mod_array['dirs'], 'ar_cmp'); // compare directory names
if( count($mod_array['files']) &gt; 1 )
usort($mod_array['files'], 'ar_cmp'); // compare filenames

<i> </i> return $mod_array;
}

<i> </i>$_SESSION['listimages']= $file_struct = recur_dir('Art');
echo '&lt;pre&gt;';
print_r($file_struct);
echo '&lt;/pre&gt;';

// To see the files or directories at any particular level (which I think was your
// original objective, simply reference $_SESSION['listimages'][level]['dirs' or 'files']
// level == 0 will be the root and depending on the depth, each level will have its directories
// and files listed.
?&gt;
×

Success!

Help @Peuplarchie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...