473,811 Members | 3,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Some array counting math

Hi there,

I have a function to create an array of all files in a certain folder,
so i can display the structure.
The actual function is below the message, as is an example of its
output.

As one can see, the filesize is also stored in the array (more info
will be added, like filemtime),
what i need is to sum all bytes per folder.

So if i have e.g.

- folder_1_level1 -size file 1 / 96
* file_1
* file_2

- folder_1_level2 -size file 3, 4, 7, 8, 9
* file_3
* file_4
- folder_1_level3 -size file 7, 8, 9
* file_7
* file_8
* file_9

- folder_2_level2 -size file 5, 6
* file_5
* file_6
The latter should give me the sum of all filesizes in there,
folder_level2 should give me folder_level3's size PLUS other files/
folders that are in folder_level2, and then, folder_level1 should have
the size of all files in there down the entire tree.

I hope it's clear and that someone can help me.

Cheers.
*** FUNCTION CODE ***

function mapit( $dir = '.', $loop = 0, $parent = false ){
$handle = @opendir( $dir );
while( ( $file=readdir( $handle ) ) !== false ){
if( $file != '.' && $file != '..' ){
$point = $dir."/".$file;
$fileSize = filesize( $dir.'/'.$file );
if( is_dir( $point ) )
$info['dirs'][$file] = mapit( $point , $loop+1, $file );
else
$info['files'][$file] = $parent.' -'.$fileSize;
}
}
return $info;
}

$structure = mapit();
print_r( $structure );

*** /FUNCTION CODE ***

*** RESULT EXAMPLE ***

Array
(
[dirs] =Array
(
[upload] =Array
(
[files] =Array
(
[index.htm] =upload -1391
[index.php] =upload -23
[upload.php] =upload -7733
)

[dirs] =Array
(
[img] =Array
(
[files] =Array
(
[0.jpg] =img -36866
[12.jpg] =img -218
[13.jpg] =img -36866
)
)
)
)

[files] =Array
(
[dirs] =Array
(
[just-a-dir] =>
[images] =Array
(
[files] =Array
(
[lion.jpg] =flip ->
350398
)
)
)
[files] =Array
(
[logo.pdf] =files -157764
[tiger.jpg] =files -350398
)
)
)

[files] =Array
(
[ftp.php] = -10238
[cats.php] = -8237
)
)

*** /RESULT EXAMPLE ***
Nov 27 '07 #1
4 1584
On Nov 27, 7:25 pm, frizzle <phpfriz...@gma il.comwrote:
Hi there,

I have a function to create an array of all files in a certain folder,
so i can display the structure.
The actual function is below the message, as is an example of its
output.

As one can see, the filesize is also stored in the array (more info
will be added, like filemtime),
what i need is to sum all bytes per folder.

So if i have e.g.

- folder_1_level1 -size file 1 / 96
* file_1
* file_2

- folder_1_level2 -size file 3, 4, 7, 8, 9
* file_3
* file_4
- folder_1_level3 -size file 7, 8, 9
* file_7
* file_8
* file_9

- folder_2_level2 -size file 5, 6
* file_5
* file_6

The latter should give me the sum of all filesizes in there,
folder_level2 should give me folder_level3's size PLUS other files/
folders that are in folder_level2, and then, folder_level1 should have
the size of all files in there down the entire tree.

I hope it's clear and that someone can help me.

Cheers.

*** FUNCTION CODE ***

function mapit( $dir = '.', $loop = 0, $parent = false ){
$handle = @opendir( $dir );
while( ( $file=readdir( $handle ) ) !== false ){
if( $file != '.' && $file != '..' ){
$point = $dir."/".$file;
$fileSize = filesize( $dir.'/'.$file );
if( is_dir( $point ) )
$info['dirs'][$file] = mapit( $point , $loop+1, $file );
else
$info['files'][$file] = $parent.' -'.$fileSize;
}
}
return $info;

}

$structure = mapit();
print_r( $structure );

*** /FUNCTION CODE ***

*** RESULT EXAMPLE ***

Array
(
[dirs] =Array
(
[upload] =Array
(
[files] =Array
(
[index.htm] =upload -1391
[index.php] =upload -23
[upload.php] =upload -7733
)

[dirs] =Array
(
[img] =Array
(
[files] =Array
(
[0.jpg] =img -36866
[12.jpg] =img -218
[13.jpg] =img -36866
)
)
)
)

[files] =Array
(
[dirs] =Array
(
[just-a-dir] =>
[images] =Array
(
[files] =Array
(
[lion.jpg] =flip ->
350398
)
)
)
[files] =Array
(
[logo.pdf] =files -157764
[tiger.jpg] =files -350398
)
)
)

[files] =Array
(
[ftp.php] = -10238
[cats.php] = -8237
)
)

*** /RESULT EXAMPLE ***
This should work. This function recursively goes through a given array
and adds up the sizes.
Note that I had to use regex to get the numerical size as you have
this string, '->' , in there.

function getSize($map) {
$size = 0;
foreach($map as $m) {
if(!is_array($m )) {
preg_match("/[0-9]{1,20}/", $m, $p); // regex to get the size
$size += $p[0];
} else {
$size += getsize($m);
}
}
return $size;
}

$structure = mapit();
echo getsize($struct ure['dirs']['upload']);

--
Kailash Nadh | http://kailashnadh.name
Nov 27 '07 #2
On Nov 27, 10:16 pm, Kailash Nadh <kailash.n...@g mail.comwrote:
On Nov 27, 7:25 pm, frizzle <phpfriz...@gma il.comwrote:
Hi there,
I have a function to create an array of all files in a certain folder,
so i can display the structure.
The actual function is below the message, as is an example of its
output.
As one can see, the filesize is also stored in the array (more info
will be added, like filemtime),
what i need is to sum all bytes per folder.
So if i have e.g.
- folder_1_level1 -size file 1 / 96
* file_1
* file_2
- folder_1_level2 -size file 3, 4, 7, 8, 9
* file_3
* file_4
- folder_1_level3 -size file 7, 8, 9
* file_7
* file_8
* file_9
- folder_2_level2 -size file 5, 6
* file_5
* file_6
The latter should give me the sum of all filesizes in there,
folder_level2 should give me folder_level3's size PLUS other files/
folders that are in folder_level2, and then, folder_level1 should have
the size of all files in there down the entire tree.
I hope it's clear and that someone can help me.
Cheers.
*** FUNCTION CODE ***
function mapit( $dir = '.', $loop = 0, $parent = false ){
$handle = @opendir( $dir );
while( ( $file=readdir( $handle ) ) !== false ){
if( $file != '.' && $file != '..' ){
$point = $dir."/".$file;
$fileSize = filesize( $dir.'/'.$file );
if( is_dir( $point ) )
$info['dirs'][$file] = mapit( $point , $loop+1, $file );
else
$info['files'][$file] = $parent.' -'.$fileSize;
}
}
return $info;
}
$structure = mapit();
print_r( $structure );
*** /FUNCTION CODE ***
*** RESULT EXAMPLE ***
Array
(
[dirs] =Array
(
[upload] =Array
(
[files] =Array
(
[index.htm] =upload -1391
[index.php] =upload -23
[upload.php] =upload -7733
)
[dirs] =Array
(
[img] =Array
(
[files] =Array
(
[0.jpg] =img -36866
[12.jpg] =img -218
[13.jpg] =img -36866
)
)
)
)
[files] =Array
(
[dirs] =Array
(
[just-a-dir] =>
[images] =Array
(
[files] =Array
(
[lion.jpg] =flip ->
350398
)
)
)
[files] =Array
(
[logo.pdf] =files -157764
[tiger.jpg] =files -350398
)
)
)
[files] =Array
(
[ftp.php] = -10238
[cats.php] = -8237
)
)
*** /RESULT EXAMPLE ***

This should work. This function recursively goes through a given array
and adds up the sizes.
Note that I had to use regex to get the numerical size as you have
this string, '->' , in there.

function getSize($map) {
$size = 0;
foreach($map as $m) {
if(!is_array($m )) {
preg_match("/[0-9]{1,20}/", $m, $p); // regex to get the size
$size += $p[0];
} else {
$size += getsize($m);
}
}
return $size;

}

$structure = mapit();
echo getsize($struct ure['dirs']['upload']);

--
Kailash Nadh |http://kailashnadh.name
Thank you very much for your quick help!
Not to be a nag though, but if i store other numerical values in the
array (like filemtime), wouldn't they be counted as well?

Would it be better to give each file a ['size'] key? In the best case,
i thought, it would be best to somehow perform the count at the same
time as doing the first loop, so you wouldn't have to run unnecessary
loop, or am i wrong?

Thanks a lot again though!!

(The '->' part doesn't have to be there, it was merely a visual
"guide" for me ...)
Nov 27 '07 #3
On Nov 27, 9:45 pm, frizzle <phpfriz...@gma il.comwrote:
On Nov 27, 10:16 pm, Kailash Nadh <kailash.n...@g mail.comwrote:
On Nov 27, 7:25 pm, frizzle <phpfriz...@gma il.comwrote:
Hi there,
I have a function to create an array of all files in a certain folder,
so i can display the structure.
The actual function is below the message, as is an example of its
output.
As one can see, the filesize is also stored in the array (more info
will be added, like filemtime),
what i need is to sum all bytes per folder.
So if i have e.g.
- folder_1_level1 -size file 1 / 96
* file_1
* file_2
- folder_1_level2 -size file 3, 4, 7, 8, 9
* file_3
* file_4
- folder_1_level3 -size file 7, 8, 9
* file_7
* file_8
* file_9
- folder_2_level2 -size file 5, 6
* file_5
* file_6
The latter should give me the sum of all filesizes in there,
folder_level2 should give me folder_level3's size PLUS other files/
folders that are in folder_level2, and then, folder_level1 should have
the size of all files in there down the entire tree.
I hope it's clear and that someone can help me.
Cheers.
*** FUNCTION CODE ***
function mapit( $dir = '.', $loop = 0, $parent = false ){
$handle = @opendir( $dir );
while( ( $file=readdir( $handle ) ) !== false ){
if( $file != '.' && $file != '..' ){
$point = $dir."/".$file;
$fileSize = filesize( $dir.'/'.$file );
if( is_dir( $point ) )
$info['dirs'][$file] = mapit( $point , $loop+1, $file );
else
$info['files'][$file] = $parent.' -'.$fileSize;
}
}
return $info;
}
$structure = mapit();
print_r( $structure );
*** /FUNCTION CODE ***
*** RESULT EXAMPLE ***
Array
(
[dirs] =Array
(
[upload] =Array
(
[files] =Array
(
[index.htm] =upload -1391
[index.php] =upload -23
[upload.php] =upload -7733
)
[dirs] =Array
(
[img] =Array
(
[files] =Array
(
[0.jpg] =img -36866
[12.jpg] =img -218
[13.jpg] =img -36866
)
)
)
)
[files] =Array
(
[dirs] =Array
(
[just-a-dir] =>
[images] =Array
(
[files] =Array
(
[lion.jpg] =flip ->
350398
)
)
)
[files] =Array
(
[logo.pdf] =files -157764
[tiger.jpg] =files -350398
)
)
)
[files] =Array
(
[ftp.php] = -10238
[cats.php] = -8237
)
)
*** /RESULT EXAMPLE ***
This should work. This function recursively goes through a given array
and adds up the sizes.
Note that I had to use regex to get the numerical size as you have
this string, '->' , in there.
function getSize($map) {
$size = 0;
foreach($map as $m) {
if(!is_array($m )) {
preg_match("/[0-9]{1,20}/", $m, $p); // regex to get the size
$size += $p[0];
} else {
$size += getsize($m);
}
}
return $size;
}
$structure = mapit();
echo getsize($struct ure['dirs']['upload']);
--
Kailash Nadh |http://kailashnadh.name

Thank you very much for your quick help!
np ;)
Not to be a nag though, but if i store other numerical values in the
array (like filemtime), wouldn't they be counted as well?

Would it be better to give each file a ['size'] key? In the best case,
i thought, it would be best to somehow perform the count at the same
time as doing the first loop, so you wouldn't have to run unnecessary
loop, or am i wrong?

Thanks a lot again though!!

(The '->' part doesn't have to be there, it was merely a visual
"guide" for me ...)
Yes, extra keys would cause the function not to work.
Like you said, a 'size' key would get rid of all the confusion. If you
get it there, simply get rid of the regex line and add the value of
the 'size' key to $size.

--
Kailash Nadh | http://kailashnadh.name
Nov 27 '07 #4
On Nov 27, 10:51 pm, Kailash Nadh <kailash.n...@g mail.comwrote:
On Nov 27, 9:45 pm, frizzle <phpfriz...@gma il.comwrote:
On Nov 27, 10:16 pm, Kailash Nadh <kailash.n...@g mail.comwrote:
On Nov 27, 7:25 pm, frizzle <phpfriz...@gma il.comwrote:
Hi there,
I have a function to create an array of all files in a certain folder,
so i can display the structure.
The actual function is below the message, as is an example of its
output.
As one can see, the filesize is also stored in the array (more info
will be added, like filemtime),
what i need is to sum all bytes per folder.
So if i have e.g.
- folder_1_level1 -size file 1 / 96
* file_1
* file_2
- folder_1_level2 -size file 3, 4, 7, 8, 9
* file_3
* file_4
- folder_1_level3 -size file 7, 8, 9
* file_7
* file_8
* file_9
- folder_2_level2 -size file 5, 6
* file_5
* file_6
The latter should give me the sum of all filesizes in there,
folder_level2 should give me folder_level3's size PLUS other files/
folders that are in folder_level2, and then, folder_level1 should have
the size of all files in there down the entire tree.
I hope it's clear and that someone can help me.
Cheers.
*** FUNCTION CODE ***
function mapit( $dir = '.', $loop = 0, $parent = false ){
$handle = @opendir( $dir );
while( ( $file=readdir( $handle ) ) !== false ){
if( $file != '.' && $file != '..' ){
$point = $dir."/".$file;
$fileSize = filesize( $dir.'/'.$file );
if( is_dir( $point ) )
$info['dirs'][$file] = mapit( $point , $loop+1, $file );
else
$info['files'][$file] = $parent.' -'.$fileSize;
}
}
return $info;
}
$structure = mapit();
print_r( $structure );
*** /FUNCTION CODE ***
*** RESULT EXAMPLE ***
Array
(
[dirs] =Array
(
[upload] =Array
(
[files] =Array
(
[index.htm] =upload -1391
[index.php] =upload -23
[upload.php] =upload -7733
)
[dirs] =Array
(
[img] =Array
(
[files] =Array
(
[0.jpg] =img -36866
[12.jpg] =img -218
[13.jpg] =img -36866
)
)
)
)
[files] =Array
(
[dirs] =Array
(
[just-a-dir] =>
[images] =Array
(
[files] =Array
(
[lion.jpg] =flip ->
350398
)
)
)
[files] =Array
(
[logo.pdf] =files -157764
[tiger.jpg] =files -350398
)
)
)
[files] =Array
(
[ftp.php] = -10238
[cats.php] = -8237
)
)
*** /RESULT EXAMPLE ***
This should work. This function recursively goes through a given array
and adds up the sizes.
Note that I had to use regex to get the numerical size as you have
this string, '->' , in there.
function getSize($map) {
$size = 0;
foreach($map as $m) {
if(!is_array($m )) {
preg_match("/[0-9]{1,20}/", $m, $p); // regex to get the size
$size += $p[0];
} else {
$size += getsize($m);
}
}
return $size;
}
$structure = mapit();
echo getsize($struct ure['dirs']['upload']);
--
Kailash Nadh |http://kailashnadh.name
Thank you very much for your quick help!

np ;)
Not to be a nag though, but if i store other numerical values in the
array (like filemtime), wouldn't they be counted as well?
Would it be better to give each file a ['size'] key? In the best case,
i thought, it would be best to somehow perform the count at the same
time as doing the first loop, so you wouldn't have to run unnecessary
loop, or am i wrong?
Thanks a lot again though!!
(The '->' part doesn't have to be there, it was merely a visual
"guide" for me ...)

Yes, extra keys would cause the function not to work.
Like you said, a 'size' key would get rid of all the confusion. If you
get it there, simply get rid of the regex line and add the value of
the 'size' key to $size.

--
Kailash Nadh |http://kailashnadh.name
Great, thanks a lot! I'll probably sadly enough won't have any time
before tomorrow to take a look at it, but i'm confident i can get it
to work! :)

And my remark of doing the counting loop at the same time as the
initial loop, or is that peanuts (in calculating power/speed) for PHP?

Thanks again for helping me so far!
Nov 28 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6389
by: effendi | last post by:
I wrote a simple script to remove an element of an array but I don't think this is the best way to go about it. I have a list of about five elements seperated by ";" I split the array using array.split(";") command and proceeded to update the elemment by assigning the null value to the arrayindex array=""
6
1830
by: edwardfredriks | last post by:
I'm looking for a script that, instead of counting down, can "count up" from a given date. So the output should be something like "(xx) days since (date/event)" or "(date/event) was (xx) days ago". Does anybody know where to find a script like that, or could someone code one for me? Thanks in advance. Yours,
193
9658
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I speak only of features in the spirit of C; something like object-orientation, though a nice feature, does not belong in C. Something like being able to #define a #define would be very handy, though, e.g: #define DECLARE_FOO(bar) #define...
3
16943
by: viewsonic | last post by:
Help, im a student that has to write a program for counting coins. Half of the program works but the other half doesn.t should have the following parameters. output is: Name Date total number of coins number of quarters number of dimes
2
7893
by: phjones | last post by:
Need help programming mortagage calculator for 3 different loans 7 year, 15 year and 30 year. using java array I am a beginner with Java, This is what I have so far. Need to know if I am off the path, import java.math.*;//*loan calculator import java.text.*;//*formats numbers public class 3 Mortgage loans { // declare class variable array int mortgage calculator
4
2975
toxicpaint
by: toxicpaint | last post by:
Hi, can anyone give me a hand. I'm currently displaying 4 random images at the top of a page. I did this using an array of 35 pictures and then writing them to page. The problem I have is that in theory you could get the same image 4 times. I quite often get 2 of the same picture come up. What's the easiest way of saying "once an image is assigned to a variable, take it out of the array"? Here's my code so far.. images = new Array(34);...
4
1534
by: kidko | last post by:
I'm writing a small game in Javascript (using just PNGs for graphics and DOM manipulation to move them), and most of the content is procedurally generated. This includes all of the weapons involved, and this is where the problem lies. The problem lies somewhere in here, I believe: function Weapon(weaponName, weaponDamage, weaponAccuracy, weaponAmmo, weaponRate, weaponClip) { /* === Weapon Structure === * weapon.nm - The weapon's name *...
15
1767
by: DL | last post by:
say, we have the following: // declare an array myArray = ; // short hand? same as myArray = new Array ? // populate it myArray = 0; myArray = 0; myArray = 1; myArray = 0;
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.